从C#Windows服务运行VB脚本

时间:2015-01-28 17:16:33

标签: c# vbscript windows-services

我有一个用C#编写的Windows服务,它只是在文件被放入监视文件夹时尝试执行VB脚本。虽然如果我只是双击它,脚本将启动正常,但它似乎不是由服务启动的。您将在下面的代码中看到我正在写入一个日志,似乎它认为它正在尝试启动cscript并执行脚本但奇怪的是在30秒之后它只是以退出代码0终止。

        public static void ExecVB_Script(string scriptpath)
    {
        try
        {
            WriteErrorLog("Execute VB instruction received.  Attempting to run VB script " + scriptpath);
            if (!File.Exists(scriptpath))
            {
                WriteErrorLog("Error: VB script execution Failed. Cannot find script " + scriptpath);
            }
            else
            {

                WriteErrorLog("Starting cscript.exe");
                System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
                startInfo.FileName = @"cscript.exe";
                startInfo.WorkingDirectory = ConfigurationManager.AppSettings["VBScriptExecPath"];
                WriteErrorLog("Working Directory is: " + startInfo.WorkingDirectory.ToString());
                startInfo.Arguments = string.Format(@" //B //Nologo " + scriptpath);
                WriteErrorLog("Arguements passed: "+ startInfo.Arguments);
                startInfo.RedirectStandardOutput = true;
                startInfo.RedirectStandardError = true;
                startInfo.UseShellExecute = false;
                startInfo.CreateNoWindow = true;
                System.Diagnostics.Process process = new System.Diagnostics.Process();
                process.StartInfo = startInfo;
                WriteErrorLog("Attempting to execute VB script");
                process.Start();
                process.WaitForExit();
                WriteErrorLog("cscript.exe has finished");
                WriteErrorLog(process.ExitCode.ToString());

            }
        }

日志文件输出。

28/01/2015 16:52:46: Execute VB instruction received.  Attempting to run VB script D:\utils\UPLOAD.vbs
28/01/2015 16:52:46: Starting cscript.exe
28/01/2015 16:52:46: Working Directory is: D:\utils\
28/01/2015 16:52:46: Arguements passed:  //B //Nologo D:\utils\UPLOAD.vbs
28/01/2015 16:52:46: Attempting to execute VB script
28/01/2015 16:53:16: cscript.exe has finished
28/01/2015 16:53:16: 0

该服务作为本地管理员运行,我也以本地管理员身份登录,因此似乎不太可能是访问或权限问题。此外,当手动执行VB脚本时,它还会写入文本日志,但是当C #Windows服务触发时,没有写入日志,这表明VB脚本根本没有被执行。

0 个答案:

没有答案