使用 C# 的命令行

发布于 2025-01-12 06:12:08 字数 1155 浏览 0 评论 0 原文

我正在尝试通过 C# 和命令行运行软件的批处理实用工具,但无法让它工作。当我将其复制并粘贴到命令提示符中时,我能够构建一个正确且有效的字符串,但是当我的代码尝试自动执行该步骤时,什么也没有发生;脚本只是运行到最后,什么也没有发生。

作为参考,这就是我想要做的: https://knowledge.autodesk.com/support/navisworks-products/learn-explore/caas/CloudHelp/cloudhelp/2020/ENU/Navisworks/files/GUID-14B017E2-B4A2-432F-8C2D -1F1856EECB8E-htm.html

我已经尝试过一些多次更改 StartInfo 的不同属性,但到目前为止没有任何效果。正在运行的不同目录是否存在问题?

  • “filesTextFile”是 C: 驱动器上的一个 .txt 文件,其中列出了位于共享网络驱动器 (\\{network}\) 上的文件的路径
  • cmd.exe 位于 C: 驱动器中
  • “NWBatchUtilityLoc”变量是C: 驱动器上的 .exe
  • .cs 脚本正在从 D: 驱动器运行。

这是我的代码摘录,显示了如何构建字符串以及如何使用 System.Diagnostics.Process。

var utilityCmd = $@"""{NWBatchUtilityLoc}"" /i ""{filesTextFile}"" /osd /log ""{logDir}"" /lang en-US";

var p = new Process
{
     StartInfo =
     {
         FileName = @"C:\Windows\System32\cmd.exe",
         Arguments = "/C " + utilityCmd,
     }
};

p.Start();

I'm trying to run a software's batch utility tool via C# and the command line but am not able to get it to work. I am able to build a string which is correct and works when I just copy and paste it into the command prompt, but when my code tries to do that step automatically nothing happens; the script just runs to the end and nothing happens.

For reference, this is what I'm trying to do: https://knowledge.autodesk.com/support/navisworks-products/learn-explore/caas/CloudHelp/cloudhelp/2020/ENU/Navisworks/files/GUID-14B017E2-B4A2-432F-8C2D-1F1856EECB8E-htm.html

I've tried a number of times changing the different properties of StartInfo, but nothing has worked so far. Could there be an issue with the different directories that are at play?

  • "filesTextFile" is a .txt file on the C: drive which lists paths for files located on a shared network drive (\\{network}\)
  • cmd.exe is in the C: drive
  • "NWBatchUtilityLoc" variable is the file path for an .exe on the C: drive
  • .cs script is running from the D: drive.

This is an excerpt of my code that shows how the string is being built and how I am using System.Diagnostics.Process.

var utilityCmd = $@"""{NWBatchUtilityLoc}"" /i ""{filesTextFile}"" /osd /log ""{logDir}"" /lang en-US";

var p = new Process
{
     StartInfo =
     {
         FileName = @"C:\Windows\System32\cmd.exe",
         Arguments = "/C " + utilityCmd,
     }
};

p.Start();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文