gpt4 book ai didi

c# - 从进程 ID 获取打开的 xlsx 文件名

转载 作者:太空宇宙 更新时间:2023-11-03 13:19:41 25 4
gpt4 key购买 nike

我正在尝试构建一个允许用户选择之前打开的 Excel 实例的表单。

从那里,我想提取出与打开的实例关联的 .xlsx 文件的名称。

我怀疑我必须使用 COM 才能将其从那里取出来,但我还没有找到实现它的方法。

我想的代码如下:

Process[] openApplications = Process.GetProcessesByName("excel");

foreach(Process p in openApplications)
{
///Find the associated .xlsx path and file
///...Maybe from the handle id?
}

任何帮助将不胜感激

最佳答案

您必须使用 ActiveWorkbook 属性。请引用this .

        try
{
Process[] openApplications = Process.GetProcessesByName("excel");
int proLen = openApplications.Length;
if (proLen == 0)
{
Console.WriteLine("The process does NOT exist or has exited...");
return 0;
}

foreach(Process p in openApplications)
{
//validate p for null/nothing
//get the name of the workbook using
//Use p.ActiveWorkbook.Name to get the file name.
}
return 1;
}
catch (Exception ex)
{

}

关于c# - 从进程 ID 获取打开的 xlsx 文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24801451/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com