Process.Exited 提前响应

时间:2022-10-27 22:48:26
使用process.Exited事件时, 如果打开xml文件,exited事件会提前触发。如以下代码,打开1.xml时就显示对话框。
 private Process myProcess = new Process();
    public void PrintDoc(string fileName)
    {
            myProcess.StartInfo.FileName =@"c:\1.xml";
            myProcess.EnableRaisingEvents = true;
            myProcess.Exited += new EventHandler(myProcess_Exited);
            myProcess.Start();
  }

       
    private void myProcess_Exited(object sender, System.EventArgs e)
    {
            messagebox.show("程序退出");    
    }
但是把 @"c:\1.xml"换成@"c:\1.txt"就没有问题
是不是因为infopath不是windows自带程序的原因啊!急求答案啊。

15 个解决方案

#1


将c:\1.xml换成.jpg  .exe. .doc .xsl都行,就是xml格式会导致提前退出啊!

#2


从联机帮助考过来的
using System;
using System.Diagnostics;
using System.ComponentModel;

namespace MyProcessSample
{
/// <summary>
/// Shell for the sample.
/// </summary>
class MyProcess
{
// These are the Win32 error code for file not found or access denied.
const int ERROR_FILE_NOT_FOUND =2;
const int ERROR_ACCESS_DENIED = 5;

/// <summary>
/// Prints a file with a .doc extension.
/// </summary>
void PrintDoc()
{
Process myProcess = new Process();

try
{
// Get the path that stores user documents.
string myDocumentsPath = 
Environment.GetFolderPath(Environment.SpecialFolder.Personal);

myProcess.StartInfo.FileName = myDocumentsPath + "\\MyFile.doc"; 
myProcess.StartInfo.Verb = "Print";
myProcess.StartInfo.CreateNoWindow = true;
myProcess.Start();
}
catch (Win32Exception e)
{
if(e.NativeErrorCode == ERROR_FILE_NOT_FOUND)
{
Console.WriteLine(e.Message + ". Check the path.");


else if (e.NativeErrorCode == ERROR_ACCESS_DENIED)
{
// Note that if your word processor might generate exceptions
// such as this, which are handled first.
Console.WriteLine(e.Message + 
". You do not have permission to print this file.");
}
}
}


public static void Main()
{
MyProcess myProcess = new MyProcess();
myProcess.PrintDoc();
}
}

#3


就是这样做的,只是使用xml格式就会提前触发exit()事件

#4


帮顶,测试了一下确实是这样

#5


因为xml是在其他的进程中打开的,如ie里面等,你要为它开一个新的进程的话,你可以选择打开方式->记事本 始终选择此种打开方式即可。

#6


用记事本打开不符合程序要求啊。我打开xml是通过microsoft infopath,是有界面进行编辑的啊。

#7


测试了一下,确实如此,不解

#8


请大家帮忙解决这个困难啊!

#9


请各位朋友帮助解决啊。

#10


这估计就是传说中的流文件了。。。。

#11


你的程序根他的不太一样,试着抄全了试试?

#12


帮忙顶一下...

#13


我写在上面是个简版,我完成按照msdn的示例原码写了一遍,如果是xml就不行,但如果是其他的大部分文件都行。

#14


是不是xml和html之类的需要浏览器的文件都不可以啊?

#15


我成功了,博客园的朋友解答的
 static public void PrintDoc(string fileName)
        {
            myProcess.StartInfo.FileName = @"infopath";
            myProcess.StartInfo.Arguments = @"c:\1.xml";
            myProcess.EnableRaisingEvents = true;
            myProcess.Exited += new EventHandler(myProcess_Exited);
            myProcess.Start();
        } 
成功!
其实一开始程序就是通过infopath打开的。但是由于xml由默认IE先打开。导致过程是这样的:
1.调用IE异步启动(但不打开文件,由于我默认是由infopath打开),导致process退出。
2.然后再用infopath打开文件,这时process以为它已经退出了。
现在通过程序改完后,就跳过IE那一关了!

#1


将c:\1.xml换成.jpg  .exe. .doc .xsl都行,就是xml格式会导致提前退出啊!

#2


从联机帮助考过来的
using System;
using System.Diagnostics;
using System.ComponentModel;

namespace MyProcessSample
{
/// <summary>
/// Shell for the sample.
/// </summary>
class MyProcess
{
// These are the Win32 error code for file not found or access denied.
const int ERROR_FILE_NOT_FOUND =2;
const int ERROR_ACCESS_DENIED = 5;

/// <summary>
/// Prints a file with a .doc extension.
/// </summary>
void PrintDoc()
{
Process myProcess = new Process();

try
{
// Get the path that stores user documents.
string myDocumentsPath = 
Environment.GetFolderPath(Environment.SpecialFolder.Personal);

myProcess.StartInfo.FileName = myDocumentsPath + "\\MyFile.doc"; 
myProcess.StartInfo.Verb = "Print";
myProcess.StartInfo.CreateNoWindow = true;
myProcess.Start();
}
catch (Win32Exception e)
{
if(e.NativeErrorCode == ERROR_FILE_NOT_FOUND)
{
Console.WriteLine(e.Message + ". Check the path.");


else if (e.NativeErrorCode == ERROR_ACCESS_DENIED)
{
// Note that if your word processor might generate exceptions
// such as this, which are handled first.
Console.WriteLine(e.Message + 
". You do not have permission to print this file.");
}
}
}


public static void Main()
{
MyProcess myProcess = new MyProcess();
myProcess.PrintDoc();
}
}

#3


就是这样做的,只是使用xml格式就会提前触发exit()事件

#4


帮顶,测试了一下确实是这样

#5


因为xml是在其他的进程中打开的,如ie里面等,你要为它开一个新的进程的话,你可以选择打开方式->记事本 始终选择此种打开方式即可。

#6


用记事本打开不符合程序要求啊。我打开xml是通过microsoft infopath,是有界面进行编辑的啊。

#7


测试了一下,确实如此,不解

#8


请大家帮忙解决这个困难啊!

#9


请各位朋友帮助解决啊。

#10


这估计就是传说中的流文件了。。。。

#11


你的程序根他的不太一样,试着抄全了试试?

#12


帮忙顶一下...

#13


我写在上面是个简版,我完成按照msdn的示例原码写了一遍,如果是xml就不行,但如果是其他的大部分文件都行。

#14


是不是xml和html之类的需要浏览器的文件都不可以啊?

#15


我成功了,博客园的朋友解答的
 static public void PrintDoc(string fileName)
        {
            myProcess.StartInfo.FileName = @"infopath";
            myProcess.StartInfo.Arguments = @"c:\1.xml";
            myProcess.EnableRaisingEvents = true;
            myProcess.Exited += new EventHandler(myProcess_Exited);
            myProcess.Start();
        } 
成功!
其实一开始程序就是通过infopath打开的。但是由于xml由默认IE先打开。导致过程是这样的:
1.调用IE异步启动(但不打开文件,由于我默认是由infopath打开),导致process退出。
2.然后再用infopath打开文件,这时process以为它已经退出了。
现在通过程序改完后,就跳过IE那一关了!