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;
  }

  

posted @ 2016-10-09 10:08  今天是周一  阅读(4828)  评论(0编辑  收藏  举报