Приложение зависает при открытии диалогового окна файла - PullRequest
1 голос
/ 21 апреля 2019

Я хочу, чтобы, когда пользователь нажимает на кнопку и открывает изображение, изображение копируется в другое место, а путь к файлу скопированного изображения сохраняется внутри «Properties.Settings.Default.custombgfilepath». Вот мой код:

private void Button2_Click(object sender, EventArgs e)
    {
        //check for openfile dialog result
        if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            textBox1.Text = openFileDialog1.FileName;
            //initialize destination
            string destination = @"C:\Launchicity\";
            //Get filename
            string filename = Path.GetFileName(openFileDialog1.FileName);
            //get filepath
            string filepath = destination + filename;
            if (!File.Exists(filepath))
            {
                File.Copy(openFileDialog1.FileName, filepath);
                Properties.Settings.Default.custombgfilepath = filepath;
                Properties.Settings.Default.Save();
            }
            else
            {
                Properties.Settings.Default.custombgfilepath = filepath;
                Properties.Settings.Default.Save();
            }
        }
    }

Вот код, автоматически сгенерированный конструктором форм Windows для openfiledialog1:

    // 
    // openFileDialog1
    // 
        this.openFileDialog1.Filter = "\"PNG (*.png)|*.png|JPG (*.jpg)|*.jpg\"";
        this.openFileDialog1.Title = "Browse Image";
        this.openFileDialog1.FileOk += new System.ComponentModel.CancelEventHandler(this.OpenFileDialog1_FileOk);

Однако, когда я нажимаю на кнопку, приложение зависает.

Можетпожалуйста, помогите мне?

Спасибо

1 Ответ

0 голосов
/ 24 апреля 2019

Со мной еще вопросы.Пожалуйста, пришлите мне отзыв;) попробуйте это:

(отредактировано)

using system.IO   
    private void button1_Click(object sender, EventArgs e)
        {
            //check for openfile dialog result
            this.openFileDialog1.Filter = "\"PNG (*.png)|*.png|JPG (*.jpg)|*.jpg\"";
            this.openFileDialog1.Title = "Browse Image";
            openFileDialog1.ShowDialog();
        }

        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {
            textBox1.Text = openFileDialog1.FileName;
            //initialize destination
            string destination = @"C:\Launchicity\";
            //Get filename
            string filename = Path.GetFileName(openFileDialog1.FileName);
            //get filepath
            string filepath = destination + filename;
            if (!File.Exists(filepath))
            {
                File.Copy(openFileDialog1.FileName, filepath);
                //Properties.Settings.Default.custombgfilepath = filepath; i dont have custombgfilepath
                Properties.Settings.Default.Save();
            }
            else
            {
                //Properties.Settings.Default.custombgfilepath = filepath; i dont have custombgfilepath
                Properties.Settings.Default.Save();
            }
        }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...