【问题标题】:Run an External Program or Batch File From a C# Xamarin Program从 C# Xamarin 程序运行外部程序或批处理文件
【发布时间】:2023-01-11 12:13:08
【问题描述】:

我想让我的 C# (Xamarin) 程序运行 EXE 或批处理 (BAT) 文件。用户将运行我的程序,并单击几个按钮之一,其中一些按钮打开网页,另一些按钮运行外部程序。这些文件将与运行主程序的计算机位于同一台计算机上,不需要更高的权限。整体程序会在Windows、UWP中。

我已经有了从数据库中提取信息的代码,上面写着“用户单击的按钮引用了一个程序,它是(例如)C:\Tools\MyTool.exe”。 (实际路径更像是 (C:\Users\Me\source\repos\ProductNameV2\ProductName\ProductName.UWP\Assets\EXE\whatever.exe"。)我使用了一个“demo.bat”文件,其中只包含 echo 和 pause语句,或对内置 Windows 程序(如记事本或 Calc)的引用,普通命令提示符可以在没有显式路径的情况下识别(即,这是已识别系统路径的一部分)。是的,虚拟文件​​的真实路径确实存在;我检查过。我还明确地将文件 demo.bat 和 dummy.txt 添加到我的 C# 项目中。

到目前为止,这大致是我实际运行批处理文件或 EXE 或只是尝试打开文本文件的尝试。什么都不管用。

1)
bool check = await Launcher.CanOpenAsync(@"file:///C:\Tools\demo.bat"); // Returns false.
bool check = await Launcher.CanOpenAsync(@"file:///C:\Tools\dummy.txt"); // Returns true.
await Launcher.OpenAsync(@"file:///C:\Tools\demo.bat") // Seems to do nothing; silently fails.
await Launcher.OpenAsync(@"file:///C:\Tools\dummy.txt") // Same.

2)
Process batchProcess = new Process();
batchProcess.StartInfo.FileName = @"file:///C:\Tools\demo.bat"; // Same result with notepad.exe
batchProcess.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
batchProcess.Start();
batchProcess.WaitForExit();
// Result: "Access is denied" error during Start().

3)
var otherProcessInfo = new ProcessStartInfo(@"file:///C:\Tools\demo.bat")
var otherProcess = Process.Start(otherProcessInfo);
otherProcess.WaitForExit();
otherProcess.Close();
// Result: "The system cannot find the file specified" despite it being the same path as in previous examples.
// Also tried literally using the path C:\Tools\demo.bat, without adding that to the C# project.
// One thing that slightly works is to use:
var otherProcessInfo = new ProcessStartInfo("cmd.exe", "/c echo Hello world!");
// This version opens a window and instantly closes it again. With "/c pause" instead, it opens, saying "press any key to continue".
// Chaining multiple commands with newline or semicolon characters doesn't work as a form of batch file.

所以:我在这里取得的唯一小成功是运行 cmd.exe,运行单行命令。我想根据批处理文件必须执行的操作,有可能接收一个字符串,将其分成几行,然后使用方法 3 一次运行 cmd.exe 来调用它们。这充其量是丑陋的。

有没有更好的方法来做到这一点——从我的程序中运行批处理文件或 EXE?

编辑:是的,我确实在询问之前查看了文档。我为什么使用 URI?由于多个错误告诉我我使用的简单路径字符串(“C:\this\that”)是“无效的 URI 格式”。使用 Process.Start("notepad.exe") 静默失败,什么都不做。使用涉及 System.Diagnostics.Process 的方法(在 How to run external program via a C# program? 找到,是的,我之前看到过)在使用我的批处理文件引用时失败并出现“拒绝访问”错误,或者使用普通的旧 notepad.exe 静默失败(没有窗口打开) .我避免设置说隐藏窗口的进程选项。

所以换句话说:有没有办法让我的程序在计算机上的某个地方运行一些 EXE,或者运行一个包含多个命令的批处理文件?那是什么方式?

【问题讨论】:

    标签: c# windows batch-file xamarin cmd


    【解决方案1】:

    使用您收集的数据,我能够通过执行以下操作来运行批处理文件:

        var otherProcessInfo = new ProcessStartInfo("cmd.exe", "/c call "C:\Users\YourUserAccountName\Desktop\Test.bat"");
        var otherProcess = Process.Start(otherProcessInfo);
        otherProcess.WaitForExit();
        otherProcess.Close();
    

    我还认为回顾 cmd.exe 应用程序的功能会很有帮助。 我发现这篇文章很有帮助: https://stackoverflow.com/questions/515309/what-does-cmd-c-mean#:~:text=%2FC%20Carries%20out%20the%20command%20specified%20by%20the%20string%20and,switches%20by%20typing%20cmd%20%2F%3F%20

    特别是 /k 选项将使窗口保持打开状态,如果您不希望它在运行脚本后关闭的话。

    非常感谢您的提问!它真的帮助我找到了这个问题的答案! (至少对于我的 .NET MAUI windows 应用程序的情况,但是 MAUI 是基于 Xamarin.Forms 构建的,所以你做同样的事情应该没有问题)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-07
      • 1970-01-01
      • 2021-10-24
      • 2018-08-25
      • 2014-06-01
      • 2022-01-06
      相关资源
      最近更新 更多