Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何处理WinForms应用程序中的Word关闭事件?_C#_.net_Winforms_Ms Word - Fatal编程技术网

C# 如何处理WinForms应用程序中的Word关闭事件?

C# 如何处理WinForms应用程序中的Word关闭事件?,c#,.net,winforms,ms-word,C#,.net,Winforms,Ms Word,我有一个WinForms应用程序,我正在寻找一种方法来执行以下操作: 单击“链接”从数据库中的BLOB创建Word文档并将其打开 阻止WinForms应用程序,直到Word关闭 关闭Word时处理,检查文档是否已更改,并将更改保留到数据库中 我遇到的问题不是创建Word文档并打开它,而是连接到Word进程以了解Word何时关闭。是否有一些库可以用来完成此任务,或者有任何教程可以演示如何完成此任务 请参阅已接受的解决方案,但以下是我用于完成任务的代码: protected static strin

我有一个WinForms应用程序,我正在寻找一种方法来执行以下操作:

  • 单击“链接”从数据库中的BLOB创建Word文档并将其打开
  • 阻止WinForms应用程序,直到Word关闭
  • 关闭Word时处理,检查文档是否已更改,并将更改保留到数据库中
  • 我遇到的问题不是创建Word文档并打开它,而是连接到Word进程以了解Word何时关闭。是否有一些库可以用来完成此任务,或者有任何教程可以演示如何完成此任务

    请参阅已接受的解决方案,但以下是我用于完成任务的代码:

    protected static string FilePath { get; set; }
    
    public static void DisplayDocument(byte[] documentBytes, string filePath)
    {
        FilePath = filePath;
    
        if (documentBytes == null) return;
    
        if (!Directory.Exists(TEMP_FILE_DIRECTORY))
            Directory.CreateDirectory(TEMP_FILE_DIRECTORY);
    
        if (File.Exists(FilePath)) File.Delete(FilePath);
    
        try
        {
            FileStream fs = new FileStream(FilePath, FileMode.Create);
            fs.Write(documentBytes, 0, Convert.ToInt32(documentBytes.Length));
            fs.Seek(0, SeekOrigin.Begin);
            fs.Close();
    
            ProcessStartInfo psi = new ProcessStartInfo(FilePath);
            Process process = Process.Start(psi);
            process.EnableRaisingEvents = true;
            process.Exited += process_Exited;
    
            process.WaitForExit();    
        }
        catch (Exception e)
        {
            MessageHandler.Show(e.Message, Strings.ErrorOpeningFile);
        }
    }
    
    private static void process_Exited(object sender, EventArgs e)
    {
        FileInfo fileInfo = new FileInfo(FilePath);
        if(fileInfo.CreationTime.CompareTo(fileInfo.LastWriteTime) < 0)
            Debug.WriteLine("File updated, perform database upload here."); 
    }
    
    受保护的静态字符串文件路径{get;set;}
    公共静态void DisplayDocument(字节[]文档字节,字符串文件路径)
    {
    FilePath=FilePath;
    if(documentBytes==null)返回;
    如果(!Directory.Exists(临时文件目录))
    CreateDirectory(临时文件目录);
    如果(File.Exists(FilePath))File.Delete(FilePath);
    尝试
    {
    FileStream fs=newfilestream(FilePath,FileMode.Create);
    写入(documentBytes,0,Convert.ToInt32(documentBytes.Length));
    fs.Seek(0,SeekOrigin.Begin);
    fs.Close();
    ProcessStartInfo psi=新的ProcessStartInfo(文件路径);
    过程=过程启动(psi);
    process.EnableRaisingEvents=true;
    process.Exited+=进程_已退出;
    process.WaitForExit();
    }
    捕获(例外e)
    {
    Show(e.Message,Strings.ErrorOpeningFile);
    }
    }
    私有静态无效进程\u已退出(对象发送方,事件参数e)
    {
    FileInfo FileInfo=新的FileInfo(FilePath);
    if(fileInfo.CreationTime.CompareTo(fileInfo.LastWriteTime)<0)
    WriteLine(“文件已更新,在此处执行数据库上载”);
    }
    
    您可以使用以下代码等待进程关闭:

    process.WaitForExit();
    

    word关闭时,您可以检查文件是否已修改并将其存储在数据库中。

    是的,这可能是您的最佳选择。Word具有打开和关闭文档时触发的内部事件,但您必须编写Word加载项或Word VBA宏才能获取这些事件。或者,您可以实例化Word.APPLICATION对象,使其可见并在其中打开文档进行编辑。在这种情况下,您可以访问(尽管是跨进程的)各种Word对象,然后可以截获它们的事件,就像您截获任何COM对象事件一样。当您启动时,回答问题是必不可少的。