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

发布于 2024-10-05 11:16:39 字数 1503 浏览 1 评论 0原文

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

  1. 单击链接以从数据库中的 BLOB 创建 Word 文档并打开它。
  2. 阻止 WinForms 应用程序,直到 Word 关闭。
  3. 处理 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."); 
}

I have a WinForms application and I am looking for a way to do the following:

  1. Click link to create a Word document from a BLOB in the database and open it.
  2. Block the WinForms application until Word is closed.
  3. Handle when Word is closed, check if the document was changed, and persist changes to the database.

The problem I am having is not creating the Word document and opening it, but it is hooking to the Word process to know when Word has closed. Is there some libraries to look at for doing this or any tutorials that would show how to accomplish this task?

Please see accepted solution, but here is the code I used to complete my task:

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."); 
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岛徒 2024-10-12 11:16:39

您可以使用以下代码等待进程关闭:

process.WaitForExit();

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

You can wait for a process to close using the following code:

process.WaitForExit();

when word will close, you can check if the file is modified and store it in database.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文