Я пытаюсь использовать 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 ().
Кто-нибудь знает, что может быть не так? Я просто пытаюсь позволить пользователю сохранить объект для последующей десериализации.
Я использую Visual Studio 2019 Professional на Windows 10.
Вот трассировка стека:
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