OpenFileDialog causes WPF app to crash

狂风中的少年 提交于 2021-01-28 06:19:52

问题


In my WPF Application I used OpenFileDialog to select an image and load it to app, this works fine as expected.

But if I run same app from a flash drive, image loades after that UI freezes, any clicks on UI makes app to crash.

I have admin manifest to app also.


回答1:


I couldn't find a good explanation but I solved this problem setting the InitialDirectory with a valid local path (e.g., Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)




回答2:


I've seen something similar to this before when running from a network drive. If the application is not being loaded from a completely trusted source, you can get a SecurityException.

In any case, try adding a try/catch block around the code that fails to see if you are getting an exception.




回答3:


Here In this case OpenFileDialog will cause the app to hang and crash.

So moved OpenFileDialog to a new thread. And everything works fine.




回答4:


I found that this problem occurs (crash) not only in WPF, but for WinForms. It is hard to say what is the source of the problem, but still it appears that Microsoft dll related to OpenFileDialog has bugs (for me, it was CmnDlg32.dll)

The only way I could call ShowDialog() function was to wrap it in the event and call with the help of

this.BeginInvoke(
        new Action<YourObject, EventArgs>(YourObject_FileDialogOpened), new object[] 
                                                        { YourObjectInstance, e });

where "this" is a Control (for example, Form).

BeginInvoke(...) grants that you call will be process in a proper way.

Problem will not appear if you use call of the OpenFileDialog under button click event or any other similar scenario.




回答5:


How Winform crash when using OpenFileDialog

using(var ofd = new OpenFileDialog())
{
   ofd.Filter = "Image Files (*.png;*.bmp;*.jpg)|*.png;*.bmp;*.jpg";
   if(ofd.ShowDialog() == DialogResult.OK) // <-- reason of crashing
   {
     PictureBox.Image = Image.FromFile(ofd.FileName);
   }
}

How to fix the issue

using(var ofd = new OpenFileDialog())
{
   ofd.Filter = "Image Files (*.png;*.bmp;*.jpg)|*.png;*.bmp;*.jpg";
   DialogResult Action = a.ShowDialog();
   if(Action == DialogResult.OK) // <-- To fix
   {
     PictureBox.Image = Image.FromFile(ofd.FileName);
   }
}



回答6:


use something like this:

Dispatcher.Invoke(new Action(() =>
            {
                using (SaveFileDialog fd = new SaveFileDialog())
                {
                    var json = JsonConvert.SerializeObject(arScene, Formatting.Indented);

                    var bytes = UTF8Encoding.UTF8.GetBytes(json); // or any byte array data

                    fd.Filter = "JSon files (*.json)|*.json|All files (*.*)|*.*|ARScene (*.ARScene)|*.ARScene";
                    fd.Title = "Save an ARScene File";
                    fd.AutoUpgradeEnabled = true;
                    fd.DefaultExt = "ARScene";
                    fd.OverwritePrompt = false;
                    fd.RestoreDirectory = true;
                    fd.SupportMultiDottedExtensions = true;
                    fd.CreatePrompt = false;

                    if (fd.ShowDialog() == DialogResult.OK)
                    {
                        if (fd.FileName != "")
                        {
                            FileStream fs = (FileStream)fd.OpenFile();
                            if (fs != null)
                            {
                                fs.Write(bytes, 0, bytes.Length);
                                fs.Close();
                            }

                        }
                    }
                    fd.Dispose(); // not needed, but save;-)
                }
}));


来源:https://stackoverflow.com/questions/13656078/openfiledialog-causes-wpf-app-to-crash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!