【问题标题】:Issue with running a bat file programatically from a com interop dll on classic asp从经典 asp 中的 com interop dll 以编程方式运行 bat 文件的问题
【发布时间】:2011-07-08 19:08:43
【问题描述】:

我正面临着使用 system.diagnostics.process 对象运行 bat 文件的困境。我有在我编译为 dll 的类库中运行 bat 文件的代码。我通过使用编译时注册它使 dll com 具有互操作性。我使用强密钥对其进行签名,导出类型库并使用 regasm 和 gacutil 命令将 dll 放入 GAC。然后,我在 dll 中创建了特定类的对象,该对象具有使用 vbscript 中的 server.createobject 方法执行 bat 文件的方法。然后我调用了bat执行的方法。方法可以正常调用,但是没有弹出 cmd 提示符,也没有执行 bat 文件。我检查了互操作 dll 是否有问题,但 dll 在 VB6 代码中运行良好。有人可以帮我解决这个问题吗?我不确定它是否在 IIS 服务器上存在一些权限问题。还是无法通过 ASP 上的 vbscript for dll 执行 cmd?

谢谢, 地理位置。

【问题讨论】:

标签: .net com interop asp-classic cmd


【解决方案1】:

显然您可能需要运行 cmd 然后将输入注入其中:

// Get the full file path
string strFilePath = "c:\\temp\\test.bat";

// Create the ProcessInfo object
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo("cmd.exe");
psi.UseShellExecute = false; 
psi.RedirectStandardOutput = true;
psi.RedirectStandardInput = true;
psi.RedirectStandardError = true;
psi.WorkingDirectory = "c:\\temp\\";

// Start the process
System.Diagnostics.Process proc = System.Diagnostics.Process.Start(psi);

// Open the batch file for reading
System.IO.StreamReader strm = System.IO.File.OpenText(strFilePath);

// Attach the output for reading
System.IO.StreamReader sOut = proc.StandardOutput;

// Attach the in for writing
System.IO.StreamWriter sIn = proc.StandardInput;

// Write each line of the batch file to standard input
while(strm.Peek() != -1) {
  sIn.WriteLine(strm.ReadLine());
}

strm.Close();

// Exit CMD.EXE
string stEchoFmt = "# {0} run successfully. Exiting";

sIn.WriteLine(String.Format(stEchoFmt, strFilePath));
sIn.WriteLine("EXIT");

// Close the process
proc.Close();

// Read the sOut to a string.
string results = sOut.ReadToEnd().Trim();

// Close the io Streams;
sIn.Close(); 
sOut.Close();

http://codebetter.com/brendantompkins/2004/05/13/run-a-bat-file-from-asp-net/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-16
    • 1970-01-01
    • 2012-01-19
    • 1970-01-01
    • 2013-08-24
    • 2012-05-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多