在Windows窗体应用程序的单个exe中嵌入批处理文件 - c# [英] Embed batch file within single exe of windows form application - c#

查看:67
本文介绍了在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());
}
}

解决方案

您好NSDZE,


您可以在项目中创建一个文件夹来存储".bat"。文件,如下所示:



然后参考代码:

 proc = new Process(); 
proc.StartInfo.FileName = AppDomain.CurrentDomain.BaseDirectory + @" ../../ bat / test.bat" ;;
proc.StartInfo.CreateNoWindow = false;
proc.StartInfo.Verb =" runas" ;;
proc.Start();
proc.WaitForExit();
MessageBox.Show(" Bat file execution !!");

问候,


Kyle


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());
        }
    }

解决方案

Hi NSDZE,

You can create a folder in the project to store ".bat" file, just as shown follows:

Then refer to the code:

    proc = new Process();
    proc.StartInfo.FileName = AppDomain.CurrentDomain.BaseDirectory + @"../../bat/test.bat";
    proc.StartInfo.CreateNoWindow = false;
    proc.StartInfo.Verb = "runas";
    proc.Start();
    proc.WaitForExit();
    MessageBox.Show("Bat file executed !!");

Regards,

Kyle


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆