C# 定制某个文件关闭的消息

来源:互联网 发布:魔盒软件下载 编辑:程序博客网 时间:2024/05/12 03:42

定制在程序打开的某个文件关闭时的Exited消息

先新建类:

    class FilesProcess

    {

        public Process myProcess= new Process();

        public string sourceFullName;    //存储ctrlfullname

 

        // dakaiwenjian

        public void OpenFile(stringfileName)

        {

            try

            {

                myProcess.StartInfo.FileName=fileName;

                myProcess.StartInfo.Verb="Open";

                myProcess.StartInfo.CreateNoWindow=true;

                myProcess.EnableRaisingEvents =true;

                myProcess.Start();

            }

            catch(Exceptionex)

            {

                MessageBox.Show("异常:"+ex.Message);

                return;

            }

        }

}

再在程序中利用该类:假设在窗口类FormMyForm中用

1、   FormMyForm中添加成员:private FilesProcess openFilesProcess = newFilesProcess();

2、   在FormMyForm的构造函数中接收事件:openFilesProcess.myProcess.Exited +=new EventHandler(fileProcess_Exited);  

3、   在FormMyForm中添加消息处理函数:private void fileProcess_Exited(objectsender,EventArgse);

4、   应用openFilesProcess.OpenFile(fileName);打开文件

这样当fileName关闭时就可以执行fileProcess_Exited。