【问题标题】:Embed batch file within single exe of windows form application - c# [duplicate]在 Windows 窗体应用程序的单个 exe 中嵌入批处理文件 - c# [重复]
【发布时间】:2019-05-09 11:02:45
【问题描述】:

我可以通过安装 costura.fody 创建我的 winform 应用程序的单个 exe。这有助于我将项目中使用的所有引用(dll 文件)绑定到单个 exe 文件。

现在我想在用户单击我的 winform 应用程序上的按钮时执行批处理文件。我可以通过下面的代码来实现它。但是批处理文件的完整路径在这里是硬编码的。

我可以将此批处理文件嵌入到我的单个 exe 中吗?如果那么这个批处理文件的路径应该是什么?

  private void bt_uninstall_Click(object sender, EventArgs e)
    {
        Process proc = null;
        try
        {
            string batDir = string.Format(@"C:\Users\Abram\Documents\Visual Studio 2017\Projects\TestApp\TestApp\DriverRegistration");
            proc = new Process();
           proc.StartInfo.WorkingDirectory = batDir;
            proc.StartInfo.FileName = "runDriver.bat";
            proc.StartInfo.CreateNoWindow = false;
            proc.StartInfo.Verb = "runas";
            proc.Start();
            proc.WaitForExit();
            MessageBox.Show("Bat file executed !!");
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.StackTrace.ToString());
        }
    }

【问题讨论】:

  • @OwenPauling 在上面的链接中使用 AppDomain.CurrentDomain.BaseDirectory 时,路径(batDir)将释放文件夹,并且由于没有 .bat 文件系统抛出错误。我的疑问是我想在 exe 中绑定批处理文件并需要指定其路径
  • @user2431727 你想把bat文件捆绑在单个exe中吗?
  • 你为什么使用 RunAs(管理员可以禁用)。批准的方式是嵌入清单。 CSC 具有与 VBC 相同的开关。见pastebin.com/KYUgEKQv
  • @gunnerone 是的,有可能吗?如果那么我该如何指定路径?

标签: c# winforms batch-file exe


【解决方案1】:

如果你想绑定exe,为什么不指定exe的路径呢?还是指定exe?

类似:

string path = AppDomain.CurrentDomain.BaseDirectory;
string pathExe = path + "yourExeName.exe\\";

【讨论】:

  • 如上尝试时,我可以通过安装 exe 来运行批处理文件 - 仅当 exe 文件放置在“bin/release”文件夹中时。当我将此 exe 文件复制到桌面或其他位置并尝试运行批处理文件时显示异常。 "System.Diagonostics.Process.StartWithShellExecuteEx(ProecessStartInfo stratinfo) at bt_uninstall_Click(object sender, EventArgs e)"
猜你喜欢
  • 1970-01-01
  • 2015-04-29
  • 1970-01-01
  • 2017-08-06
  • 2010-10-07
  • 2018-07-12
  • 1970-01-01
  • 2021-06-21
  • 1970-01-01
相关资源
最近更新 更多