我有一堆CHM文件,想进行重命名,修改后的名称与文件的标题相同,写了几行代码,不管是文本文件还是CHM文件都一个毛病,循环到第二遍就取不来MainWindowTitle的值。以下是代码,请大侠们帮帮忙!
private void button5_Click(object sender, System.EventArgs e)
{
for (int i=1;i<4;i++)
{
System.Diagnostics.Process aa;
aa = System.Diagnostics.Process.Start(@"C:\新建文件夹\"+i.ToString()+".txt");
MessageBox.Show(aa.MainWindowTitle.ToString());
aa.Kill();
}

}

解决方案 »

  1.   

    请看MSDN中的说明A process has a main window associated with it only if the process has a graphical interface. If the associated process does not have a main window (so that MainWindowHandle is zero), MainWindowTitle is an empty string (""). If you have just started a process and want to use its main window title, consider using the WaitForInputIdle method to allow the process to finish starting, ensuring that the main window handle has been created. Otherwise, the system throws an exception.
      

  2.   

    所以你只要在 aa = Process.Start(...);后面加上一行 aa.WaitForInputIdle();然后再取那个参数就可以了
      

  3.   

    不过你这样做很危险,比方 .txt 的应用程序已经打开的情况你再 start... 就会直接结束进程,根本得不到窗口标题