0

我的 WCF 服务中有类来执行批处理文件。当我测试在共享目录中运行批处理文件时,一切都很好,批处理已执行,但是当我尝试从安全目录运行批处理文件时,我收到错误"ACCESS DENIED"。如何添加登录属性以便我可以访问我的安全目录来执行我的批处理文件?

这是我的代码:

public string ExecuteBat()
{   
  string hasil = "";
  ProcessStartInfo processInfo = new ProcessStartInfo(@"D:\Secure\command.bat");
  processInfo.CreateNoWindow = true;
  processInfo.UseShellExecute = false;
  Process process = Process.Start(processInfo);
  process.WaitForExit();
  if (process.ExitCode == 0) 
  { 
    hasil = "BAT EXECUTED!"; 
  }
  else 
  { 
    hasil = "EXECUTE BAT FAILED"; 
  }
  return hasil;
}
4

1 回答 1

1

该类ProcessStartInfo具有 的属性DomainUserName并且Password当设置时,在这些凭据下启动进程,如下所示:

ProcessStartInfo processInfo = new ProcessStartInfo(@"D:\Rpts\SSIS_WeeklyFlash_AAF_1.bat");
processInfo.CreateNoWindow = true;
processInfo.UseShellExecute = false;
processInfo.Domain= "MyCompanyDomain";
processInfo.UserName = "username";
//Secure string is an odd beast, so you need something like this:
SecureString ss = new SecureString();
string password = "p@$$w0rd";
foreach (char c in password)
{
  ss.AppendChar(c);
}
processInfo.Password = ss;
...
于 2012-09-05T09:36:00.503 回答