C# Process类在程序中实现以管理员方式运行外部进程运行


  因为项目中需要使用BCP命令导出数据库中数据,然后将导出结果的统计信息输出到文本中,但是bcp命令对文件夹的操作权限不足,所以就用到了Process类以管理员的身份运行BCP控制台程序,直接上代码:

  Process p = new Process();

  try
  {
    p.StartInfo.FileName = "bcp.exe";
    p.StartInfo.RedirectStandardInput = true;
    p.StartInfo.RedirectStandardOutput = true;
    p.StartInfo.RedirectStandardError = true;
    p.StartInfo.UseShellExecute = false;
    p.StartInfo.Verb = "RunAs";
    p.StartInfo.UseShellExecute = false;
    //@必须加上,不然特殊字符会被自动过滤掉
    p.StartInfo.Arguments = sql;
    p.Start();
    p.WaitForExit();
    p.Close();
  }
  catch (Exception e)
  {
    log.writeLog_DB("失败020", sql + "||||||" + e.Message.ToString());
    return false;
  }

  


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM