{
  "schemaVersion": "0.11.0",
  "canonical": "https://www.pystone.net/notes/csharp-shell-command-invocation/",
  "atlas": "https://www.pystone.net/?node=csharp-shell-command-invocation#knowledge-atlas",
  "markdown": "https://www.pystone.net/notes/csharp-shell-command-invocation.md",
  "context": "https://www.pystone.net/notes/csharp-shell-command-invocation.context.json",
  "knowledgeVersion": "224c990773de.5fa8af6e39fa",
  "build": {
    "siteCommit": "224c990773de166d23a886306577dd90379529ce",
    "notesCommit": "5fa8af6e39fa3891d1b9b4832bfa6c4e0ecaaf0a",
    "builtAt": "1970-01-01T00:00:00.000Z",
    "version": "224c990773de.5fa8af6e39fa"
  },
  "id": "note:csharp-shell-command-invocation",
  "slug": "csharp-shell-command-invocation",
  "title": "C#调用Shell命令",
  "type": "note",
  "visibility": "public",
  "idStability": "rename-stable",
  "author": {
    "name": "Perrin Yong",
    "profile": "https://www.pystone.net/profile/"
  },
  "publisher": {
    "name": "Perrin Yong",
    "profile": "https://www.pystone.net/profile/"
  },
  "aliases": [],
  "summary": "C 调用Shell命令",
  "contentRole": "unspecified",
  "isMoc": false,
  "mocRecognition": "none",
  "generated": false,
  "attribution": "unspecified",
  "domain": "10-计算机、信息技术与工程",
  "tags": [],
  "mocs": [],
  "contentHash": "e36b9360b450019fee6480d8fac6d19f2d03ae61b2f0e28e8d13743a81b29e42",
  "assets": [],
  "headings": [
    {
      "depth": 1,
      "text": "C#调用Shell命令",
      "anchor": "c调用shell命令",
      "citation": "https://www.pystone.net/notes/csharp-shell-command-invocation/#c%E8%B0%83%E7%94%A8shell%E5%91%BD%E4%BB%A4"
    },
    {
      "depth": 2,
      "text": "类Unix系统上调用bash",
      "anchor": "类unix系统上调用bash",
      "citation": "https://www.pystone.net/notes/csharp-shell-command-invocation/#%E7%B1%BBunix%E7%B3%BB%E7%BB%9F%E4%B8%8A%E8%B0%83%E7%94%A8bash"
    },
    {
      "depth": 2,
      "text": "windows系统调用CMD",
      "anchor": "windows系统调用cmd",
      "citation": "https://www.pystone.net/notes/csharp-shell-command-invocation/#windows%E7%B3%BB%E7%BB%9F%E8%B0%83%E7%94%A8cmd"
    }
  ],
  "claims": [],
  "outgoing": [],
  "incoming": [
    {
      "id": "note:programming-languages-and-runtimes",
      "title": "编程语言与运行时",
      "url": "https://www.pystone.net/notes/programming-languages-and-runtimes/",
      "atlas": "https://www.pystone.net/?node=programming-languages-and-runtimes#knowledge-atlas",
      "label": "编程语言与运行时",
      "origin": "explicit",
      "humanReviewed": true,
      "context": ".NET与CSharp中的“C#调用Shell命令”导航项",
      "citation": "https://www.pystone.net/notes/programming-languages-and-runtimes/#net%E4%B8%8Ecsharp"
    }
  ],
  "contentMarkdown": "# C#调用Shell命令\n\n> 创建时间：2020/10/23 12:10\n\n使用C#在Windows系统上调用命令行，在MacOS上调用bash，笔者在实现时遇到了很多坑，记录下来。\n以下代码亲测直接可用。在某些版本中，使用某个方法可能会出问题，选一个使用即可。\n\n## 类Unix系统上调用bash\n\n通过StartInfo.Arguments参数，只执行一行命令。\n\n```xml\npublic class UsingEnum&lt;T&gt; where T : System.Enum { }\npublic class UsingDelegate&lt;T&gt; where T : System.Delegate { }\npublic class Multicaster&lt;T&gt; where T : System.MulticastDelegate { }\n\n```\n\n通过重定向标准输入输出流，一行一行执行命令\n\n```xml\npublic class UsingEnum&lt;T&gt; where T : System.Enum { }\npublic class UsingDelegate&lt;T&gt; where T : System.Delegate { }\npublic class Multicaster&lt;T&gt; where T : System.MulticastDelegate { }\n\n```\n\n标准输入输出的方式也可以写一行，在行末尾加exit 命令，退出。\n如果不执行exit命令，调用StandardOutput.ReadToEnd，会由于无法读到终止符而阻塞进程。\n\n```xml\npublic class UsingEnum&lt;T&gt; where T : System.Enum { }\npublic class UsingDelegate&lt;T&gt; where T : System.Delegate { }\npublic class Multicaster&lt;T&gt; where T : System.MulticastDelegate { }\n\n```\n\n## windows系统调用CMD\n\n理论上与UNIX-like系统调用shell命令方式相同\n以下为一行命令执行完的方法。由于在CMD中ReadToEnd会读出多余信息，这里提供一个CMDOutputFilter函数过滤多余的信息。\n\n```xml\npublic class UsingEnum&lt;T&gt; where T : System.Enum { }\npublic class UsingDelegate&lt;T&gt; where T : System.Delegate { }\npublic class Multicaster&lt;T&gt; where T : System.MulticastDelegate { }\n\n```\n\n以luacheck工具为例，测试以上函数\n\n```csharp\nclass MyClass&lt;T, U&gt;\n    where T : class\n    where U : struct\n{ }\n\n```\n"
}
