SaveFileDialog() 试图读取或写入受保护的内存

SaveFileDialog() Attempted to read or write protected memory

我正在尝试使用 SaveFileDialog(),但在 saveFile.ShowDialog();:

上出现异常
System.AccessViolationException: 'Attempted to read or write protected memory. This is often an indication that other memory is corrupt.'

这是我的代码:

private void saveFile(ACCObject acc) {

            IFormatter formatter = new BinaryFormatter();

            SaveFileDialog saveFile = new SaveFileDialog();

            // set a default file name
            saveFile.InitialDirectory = Environment.ExpandEnvironmentVariables(@"C:\Users\%USERNAME%\Desktop\");
            saveFile.Title = "Save File";
            saveFile.FileName = "unknown.txt";
            saveFile.DefaultExt = "txt";
            saveFile.FilterIndex = 1;

            // set filters - this can be done in properties as well
            saveFile.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";


            DialogResult result = saveFile.ShowDialog();

            if (result == DialogResult.OK) {

                Stream stream = new FileStream(saveFile.FileName, FileMode.Create, FileAccess.Write);

                formatter.Serialize(stream, acc);
                stream.Close();
            }
        }

我在使用 FolderBrowserDialog() 或 OpenFileDialog() 时没有遇到任何问题。

有谁知道哪里出了问题?我只是想让用户保存对象以便稍后反序列化它。

我在 Windows 10.

上使用 Visual Studio 2019 Professional

这是堆栈跟踪:

StackTrace  "   at System.Windows.Forms.FileDialogNative.IFileDialog.Show(IntPtr parent)\r\n   at System.Windows.Forms.FileDialog.RunDialogVista(IntPtr hWndOwner)\r\n   at System.Windows.Forms.FileDialog.RunDialog(IntPtr hWndOwner)\r\n   at System.Windows.Forms.CommonDialog.ShowDialog(IWin32Window owner)\r\n   at System.Windows.Forms.CommonDialog.ShowDialog()\r\n   at com.aerocc.aerocc.saveFile(ACCObject acc) in C:\Users\user\Source\Repos\user\com-engineering-app\Calculations\aerocc\aerocc.cs:line 613\r\n   at com.aerocc.aerocc.calculate() in C:\Users\user\Source\Repos\user\com-engineering-app\Calculations\aerocc\aerocc.cs:line 425\r\n   at com.aerocc.aerocc.Preview_Click(Object sender, EventArgs e) in C:\Users\user\Source\Repos\user\com-engineering-app\Calculations\aerocc\aerocc.cs:line 341\r\n   at System.Windows.Forms.Control.OnClick(EventArgs e)\r\n   at System.Windows.Forms.Button.OnClick(EventArgs e)\r\n   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)\r\n   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)\r\n   at System.Windows.Forms.Control.WndProc(Message& m)\r\n   at System.Windows.Forms.ButtonBase.WndProc(Message& m)\r\n   at System.Windows.Forms.Button.WndProc(Message& m)\r\n   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)\r\n   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)\r\n   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)\r\n   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)\r\n   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)\r\n   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)\r\n   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)\r\n   at System.Windows.Forms.Application.Run(Form mainForm)\r\n   at com.Program.Main() in C:\Users\user\Source\Repos\user\com-engineering-app\Program.cs:line 13"    string

查看下面的内容 link 以检查如何序列化文件。另外,不清楚 ACCObject 它有什么。

https://docs.microsoft.com/en-us/dotnet/api/system.runtime.serialization.formatters.binary.binaryformatter?view=netframework-4.8