如何使用“与桌面交互”的调度程序运行Windows 2008任务

时间:2022-03-21 05:23:03

I have a small .NET app that I'm running under Windows 2008 Server via the Task Scheduler. This application needs to open an excel file and then save it as csv. The task fails when I try to open the workbook. If I run it manually without the task scheduler running it, the app works fine.

我有一个小的。net应用程序,我正在Windows 2008服务器下通过任务调度程序运行。这个应用程序需要打开一个excel文件,然后将其保存为csv。当我试图打开工作簿时,任务失败了。如果我在没有任务调度程序运行的情况下手动运行它,这个应用程序就会运行得很好。

I have it set to "Run with highest privileges" and have "Run weather user is logged on or not" checked.

我将它设置为“以最高权限运行”,并检查“运行天气用户是否已登录”。

My guess is that this process needs to interact with the desktop similar to check the "interact with desktop" flag on a service. But I have been unable to find a similar thing for scheduled tasks.

我猜想这个过程需要与桌面交互,类似于检查服务上的“与桌面交互”标志。但我一直无法找到与计划任务相似的东西。

Here is code that is failing: (it fails on the workbook.open call)

下面是正在失败的代码:(它在工作簿上失败了。打开电话)

public static void ConvertExcelToCsv(string source, string destination)
{
    if (File.Exists(destination)) File.Delete(destination);

    Application xl = new Application();

    try
    {
        Workbook workbook = xl.Workbooks.Open(source, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
        Worksheet ws = (Worksheet)workbook.Sheets[1];
        ws.SaveAs(destination, XlFileFormat.xlCSV, Type.Missing, Type.Missing, false, false, Type.Missing, Type.Missing, Type.Missing,true);

        Marshal.ReleaseComObject(ws);
    }
    finally
    {
        xl.DisplayAlerts = false;
        xl.Quit();

        Marshal.ReleaseComObject(xl);                
    }

}

1 个解决方案

#1


8  

I've had problems automating Office from a Windows Service under Windows Server 2008, even though that works fine under Windows Server 2003. The problem also occurs at the Open call, so it may be the same problem.

在Windows Server 2008下,我曾遇到过从Windows服务自动运行Office的问题,尽管这在Windows Server 2003下运行良好。这个问题也出现在Open call上,所以它可能是同一个问题。

I tried following the advice given by H Ogawa in this MSDN thread, and it seemed to work. It's bizarre, but kudos to Mr. Ogawa for discovering it.

我尝试遵循H Ogawa在这个MSDN线程中给出的建议,而且它似乎起作用了。这很奇怪,但小川庆发现了这一点值得称赞。

Summary of the 'Ogawa Hack': create a desktop folder for the system profile, as either

“小川黑客”的总结:为系统配置文件创建一个桌面文件夹

C:\Windows\SysWOW64\config\systemprofile\Desktop, or

C:\Windows\SysWOW64\config\ systemprofile \桌面,或

C:\Windows\System32\config\systemprofile\Desktop

C:\Windows\System32\config\ systemprofile \桌面

...depending on whether you have 64-bit Windows.

取决于您是否有64位窗口。

Also, the folder needs write permission for whatever user is "driving" Office.

此外,无论用户是“驾驶”办公室,文件夹都需要写入权限。

[Edit: corrected link URL]

(编辑:纠正链接URL)

#1


8  

I've had problems automating Office from a Windows Service under Windows Server 2008, even though that works fine under Windows Server 2003. The problem also occurs at the Open call, so it may be the same problem.

在Windows Server 2008下,我曾遇到过从Windows服务自动运行Office的问题,尽管这在Windows Server 2003下运行良好。这个问题也出现在Open call上,所以它可能是同一个问题。

I tried following the advice given by H Ogawa in this MSDN thread, and it seemed to work. It's bizarre, but kudos to Mr. Ogawa for discovering it.

我尝试遵循H Ogawa在这个MSDN线程中给出的建议,而且它似乎起作用了。这很奇怪,但小川庆发现了这一点值得称赞。

Summary of the 'Ogawa Hack': create a desktop folder for the system profile, as either

“小川黑客”的总结:为系统配置文件创建一个桌面文件夹

C:\Windows\SysWOW64\config\systemprofile\Desktop, or

C:\Windows\SysWOW64\config\ systemprofile \桌面,或

C:\Windows\System32\config\systemprofile\Desktop

C:\Windows\System32\config\ systemprofile \桌面

...depending on whether you have 64-bit Windows.

取决于您是否有64位窗口。

Also, the folder needs write permission for whatever user is "driving" Office.

此外,无论用户是“驾驶”办公室,文件夹都需要写入权限。

[Edit: corrected link URL]

(编辑:纠正链接URL)