当我打开文件对话框时,应用程序冻结

时间:2019-04-21 13:01:22

标签: c# winforms

我想要它,以便当用户单击按钮并打开图像时,图像被复制到另一个位置,并且复制的图像的文件路径保存在“ 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();
            }
        }
    }

这是openfiledialog1的Windows窗体设计器自动生成的代码:

    // 
    // 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 :(得分:1)

更多问题与我交谈。请给我反馈;)试试这个:

(已编辑)

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();
            }
        }