吸毒的虫子 发表于 2015-8-15 11:43:30

C#执行Shell命令,WebService需要访问其他服务器的文件时,在IIS中登入Windows用户

  WebService架在A服务器里,需要从B服务器获取数据,数据源是一些txt文件。我先用File.Exists(string path)判断这些txt文件是否存在,我的path是\\<server ip>\<folder name>\<file name>.txt的样子。在本机运行的时候,因为我已经通过登录Windows用户访问过服务器B,所以我在Visual Studio直接浏览WebService时,File.Exists()函数返回的是True。而当我把WebService架到IIS的时候,返回的则是False。通过单元测试,抛出的没有权限访问该文件的异常。通过代码,在查找文件前先登入Windows用户,问题得以解决。
  
  通过Shell命令可以登入Windows用户,不过C#对于Shell的支持不像VB,需要通过Process来实现。
  
Process p = new Process();
p.StartInfo.FileName = @&quot;net&quot;;
p.StartInfo.Arguments = @&quot;use \\<server name|server ip>\<folder name> <password> /user:<username>&quot;;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
p.WaitForExit();
p.Close();
页: [1]
查看完整版本: C#执行Shell命令,WebService需要访问其他服务器的文件时,在IIS中登入Windows用户