代码之家  ›  专栏  ›  技术社区  ›  Kelly

如何从带有“与桌面交互”的计划程序运行Windows 2008任务

  •  9
  • Kelly  · 技术社区  · 16 年前

    我有一个小的.NET应用程序,通过任务调度程序在Windows2008服务器下运行。此应用程序需要打开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);                
        }
    
    }
    
    1 回复  |  直到 15 年前
        1
  •  8
  •   Gary McGill    16 年前

    在WindowsServer2008下,我在从Windows服务自动运行Office时遇到了一些问题,尽管在WindowsServer2003下,这很好。这个问题也发生在开放式呼叫上,所以可能是同一个问题。

    我试着听从小川在年提出的建议。 this MSDN thread 而且它似乎起作用了。这很奇怪,但小川先生发现了这一点,他对此深感荣幸。

    小川黑客概要:为系统配置文件创建一个桌面文件夹,作为

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

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

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

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

    [编辑:更正的链接URL]