启动进程不能与模拟一起工作



我在调用具有模拟的批处理文件时遇到了麻烦。NET 4.0)

我有一个web应用程序,调用驻留在服务器的本地文件系统上的测试批处理文件。当我不模拟地运行它时,它运行得很好。但是使用模拟,批处理文件不会产生任何输出,也不会返回任何错误。

下面是执行我使用的批处理文件的代码-

public static bool ExecuteBatchFile(string fileLocationPath, string filePath, string arguments, TextBox textBox)
        {
            try
            {
                ProcessStartInfo procStartInfo = new ProcessStartInfo(filePath);
                procStartInfo.UseShellExecute = false;
                procStartInfo.Arguments = arguments;
                procStartInfo.WorkingDirectory = fileLocationPath;
                procStartInfo.RedirectStandardOutput = true;
                procStartInfo.RedirectStandardError = true;
                procStartInfo.RedirectStandardInput = true;
                procStartInfo.UserName = "XYZ";
                procStartInfo.Domain = "ABC";
                System.Security.SecureString pwd = new System.Security.SecureString();
                foreach (char c in "PWD")
                    pwd.AppendChar(c);
                procStartInfo.Password = pwd;
                procStartInfo.LoadUserProfile = true;
                Process proc = new Process();
                proc.StartInfo = procStartInfo;
                proc.OutputDataReceived += delegate(object sender, DataReceivedEventArgs e)
                {
                    textBox.Text += "output rcvdrn";
                    textBox.Text += e.Data;
                };
                proc.ErrorDataReceived += delegate(object sender, DataReceivedEventArgs e)
                {
                    textBox.Text += "error rcvdrn";
                    textBox.Text += e.Data;
                };
                proc.Start();
                proc.BeginOutputReadLine();
                proc.BeginErrorReadLine();
                proc.WaitForExit();
                proc.Close();
                return true;
            }
            catch (Exception e)
            {
                textBox.Text += e.Message;
                return false;
            }
        }

如果没有模拟,我可以看到批处理文件的输出。我得到输出接收批处理文件的输出,然后是一个空的OutputReceived,然后是一个空的ErrorReceived。

但是用模仿,我什么也看不见!对于没有数据的OutputReceived只有一个事件,对于没有数据的ErrorReceived只有一个事件。

我在web配置文件中设置了如下的模拟:

<identity impersonate="true" userName="ABCXYZ" password="PWD"/>

我在Windows 2003服务器上遇到过类似的问题,我的服务托管在IIS中执行应用程序。

我的发现:1. 您必须调整服务的权限,并将其配置为输出堆栈跟踪(包括. net放置构建服务的文件夹)。2. 启动加载用户配置文件的应用程序不能从服务中完成。你可以创建不加载配置文件就可以执行的包装器应用程序,该应用程序可以在加载用户配置文件的情况下执行应用程序。包装器必须在Release模式下构建,并且不能调用Trace函数。3.在服务变更上,你必须-重新启动IIS管理单元中的应用程序池-重启站点-执行iisreset.

最新更新