简体   繁体   English

调用FormClosing事件,但没有任何反应

[英]FormClosing event is called but nothing happens

I'm trying to save the file as soon as the form is closed but it does not work properly. 表单关闭后,我正在尝试保存文件,但无法正常工作。 It shows the message, but when I tell the program I want to save nothing happens, is as if it executes a return 它显示消息,但是当我告诉程序我不希望保存任何内容时,就好像它执行了返回一样

this.FormClosing += new FormClosingEventHandler(OverrideEventClosing);

private void OverrideEventClosing(object sender, FormClosingEventArgs e) {
 DialogResult dialogResult = MessageBox.Show("Do You Want To Save Your Data", "CodeJuggler", MessageBoxButtons.YesNo);
 if (dialogResult == DialogResult.Yes) {
  string myExePath = System.Reflection.Assembly.GetEntryAssembly().Location;
  System.IO.StreamWriter file = new System.IO.StreamWriter(@ "C:\DB.txt");

  foreach(ListViewItem cliente in listView1.Items) {
   file.WriteLine(string.Format("{0}|{1}|{2}|{3}", cliente.SubItems[0].Text, cliente.SubItems[1].Text, cliente.SubItems[2].Text, cliente.SubItems[3].Text));
  }
 }

}

写入后刷新并关闭流。(foreach之后)

Problem solved with unmanifested line addition 用未显示的行添加解决问题

Project > Add New Item > Application Manifest File 项目>添加新项>应用程序清单文件

Application Manifest File 申请清单文件

<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

and using this 并使用这个

string myExePath = AppDomain.CurrentDomain.BaseDirectory;
System.IO.StreamWriter File = new System.IO.StreamWriter(myExePath + @"\item.txt");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM