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

2023-09-07 09:33:11 作者:垫底

我有一个WinForms应用程序,我正在寻找一种方式来做到以下几点:

单击链接创建从数据库中的BLOB一个Word文档,并将其打开。 块的WinForms应用程序,直到字被关闭。 当Word关闭句柄,检查文件被更改,并坚持更改数据库中。

我有没有创建Word文档并打开它,但它挂接到Word的过程要知道当Word已关闭的问题。有一些图书馆看看这样做或任何教程将展示如何完成这个任务?

请参阅接受的解决方案,但在这里是code我来完成我的任务:

 受保护的静态字符串的文件路径{获得;组; }

公共静态无效DisplayDocument(byte []的documentBytes,字符串文件路径)
{
    文件路径=文件路径;

    如果(documentBytes == NULL)回报;

    如果(!Directory.Exists(TEMP_FILE_DIRECTORY))
        Directory.CreateDirectory(TEMP_FILE_DIRECTORY);

    如果(File.Exists(文件路径))File.Delete(文件路径);

    尝试
    {
        的FileStream FS =新的FileStream(文件路径,FileMode.Create);
        fs.Write(documentBytes,0,Convert.ToInt32(documentBytes.Length));
        fs.Seek(0,SeekOrigin.Begin);
        fs.Close();

        的ProcessStartInfo磅=新的ProcessStartInfo(文件路径);
        工艺过程=的Process.Start(PSI);
        process.EnableRaisingEvents = TRUE;
        process.Exited + = process_Exited;

        process.WaitForExit();
    }
    赶上(例外五)
    {
        MessageHandler.Show(e.Message,Strings.ErrorOpeningFile);
    }
}

私有静态无效process_Exited(对象发件人,EventArgs的)
{
    FileInfo的的fileInfo =新的FileInfo(文件路径);
    如果(fileInfo.CreationTime.CompareTo(fileInfo.LastWriteTime)℃,)
        的Debug.WriteLine(文件更新,在这里执行数据库上传。);
}
 

解决方案 WPF Application Management应用程序管理 1

您可以等待过程中,关闭使用以下code:

  process.WaitForExit();
 

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

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

Click link to create a Word document from a BLOB in the database and open it. Block the WinForms application until Word is closed. 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."); 
}

解决方案

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.

 
精彩推荐