本文介绍了在Windows窗体应用程序的单个exe中嵌入批处理文件 - c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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


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


我可以将这个批处理文件嵌入到我的单个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 execution !!");
}
catch(exception ex)
{
MessageBox.Show(ex.StackTrace.ToString());
}
}
解决方案

I can create single exe of my winform application by installing costura.fody. This helps me to bind all references (dll fiies) that I used in the project to a single exe file.

Now I want to execute a batch file when user clicks on a button on my winform app.  I can achieve it by below code. But the full path of batch file is hardcoded here. 

Can I embed this batch file to my single exe? If then what should be the path of this batch file? 

 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());
        }
    }
解决方案


这篇关于在Windows窗体应用程序的单个exe中嵌入批处理文件 - c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 01:43