无法使用cmd.exe

时间:2018-03-29 06:23:52

标签: c# iis angular-cli

我想使用c#代码执行ng build来构建我的角度项目。我在IIS上使用c#应用程序托管。当我执行代码时,我收到错误:

ng'无法识别为内部或外部命令,\ r \ noperable程序或批处理文件。\ r \ n

我创建了另一个我没有在IIS上托管的应用程序,我正在使用visual studio在本地执行它。在同样的代码是工作属性。我无法弄清楚iis的问题。我的代码如下。

private void buildAngular()
        {
            try
            {

                Process p = new Process();
                p.EnableRaisingEvents = true;
                p.StartInfo.FileName = "cmd.exe";
                p.StartInfo.WorkingDirectory = @"D:\AngularToBuild\AngularProject";
                p.StartInfo.UseShellExecute = false;
                //p.StartInfo.Arguments = @"/c echo off > fff1.txt";
                p.StartInfo.Arguments = @"/c ng build";
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.RedirectStandardError = true;

                p.Start();

                p.Exited += new EventHandler(myProcess_Exited);

                var err = p.StandardError.ReadToEnd();
                var msg = p.StandardOutput.ReadToEnd();

                Console.WriteLine("error", msg, err);
            }
            catch (Exception ex)
            {
                Console.WriteLine("error");
            }

        }
        private void myProcess_Exited(object sender, System.EventArgs e)
        {
            Console.WriteLine("Exit time:    {0}\r\n" +
                "Exit code:    {1}\r\nElapsed time: {2}");
        }

1 个答案:

答案 0 :(得分:1)

ng.cmd位于C:\Users\<user name>\AppData\Roaming\npm,但默认应用程序池标识无权访问该文件夹。

授予NTFS对应用程序池标识的读取权限,并确保该文件夹包含在PATH环境系统变量中。

DefaultAppPool的默认应用程序池标识为IIS APPPOOL\DefaultAppPool。您需要向此帐户授予权限。

  1. 打开Windows资源管理器
  2. 转到C:\Users\<user name>\AppData\Roaming
  3. 右键点击npm
  4. 选择Properties
  5. 选择Security标签
  6. 点击edit按钮
  7. 点击add按钮
  8. 在文本框中输入IIS APPPOOL\DefaultAppPool
  9. 点击Ok按钮
  10. 确保Read and ExecuteList Folder and ContentRead权限已经过检查。
  11. 点击Ok
  12. 点击Ok