如何使用"与桌面交互"从调度程序运行Windows 2008任务

Kel*_*lly 9 excel scheduled-tasks task

我有一个小型的.NET应用程序,我通过任务计划程序在Windows 2008 Server下运行.此应用程序需要打开一个excel文件,然后将其保存为csv.我尝试打开工作簿时任务失败.如果我在没有任务调度程序运行它的情况下手动运行它,该应用程序工作正常.

我将其设置为"以最高权限运行"并选中"运行天气用户是否已登录".

我的猜测是这个过程需要与桌面交互,类似于检查服务上的"与桌面交互"标志.但是我无法为计划任务找到类似的东西.

这是失败的代码:(它在workbook.open调用失败)

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

}
Run Code Online (Sandbox Code Playgroud)

Gar*_*ill 8

我在Windows Server 2008下从Windows服务自动化Office时遇到了问题,即使在Windows Server 2003下工作正常.问题也出现在Open调用中,因此可能是同样的问题.

我尝试按照H Ogawa在这个MSDN线程中给出的建议,它似乎工作.这很奇怪,但是对小川先生的发现感到很荣幸.

'Ogawa Hack'摘要:为系统配置文件创建一个桌面文件夹

C:\Windows\SysWOW64\config\systemprofile\Desktop, 要么

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

...取决于您是否拥有64位Windows.

此外,该文件夹需要写入权限,无论用户是什么"驾驶"Office.

[编辑:更正后的链接网址]