0

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());
        }
    }
user2431727
  • 877
  • 2
  • 15
  • 46
  • @OwenPauling as in the above link when using AppDomain.CurrentDomain.BaseDirectory , path (batDir ) is going to release folder and since there is no .bat file system throws error. What my doubt is I want to bind the batch file within the exe and need to specify its path – user2431727 May 09 '19 at 11:21
  • @user2431727 do you want the bat file bundled in the single exe? – gunnerone May 09 '19 at 14:12
  • Why are you using RunAs (which admins can disable). The approved way is to embed a manifest. CSC has the same switches as VBC. See https://pastebin.com/KYUgEKQv – Noodles May 09 '19 at 19:30
  • @gunnerone yes, is it possible? if then how can i specify the path ? – user2431727 May 10 '19 at 03:25

1 Answers1

0

If you wanna bind with the exe, why don't you specify the path to the exe? or specify the exe?

Something like:

string path = AppDomain.CurrentDomain.BaseDirectory;
string pathExe = path + "yourExeName.exe\\";
Mikev
  • 2,012
  • 1
  • 15
  • 27
  • when trying as above I can run the batch file by installing exe - only when exe file is placed in "bin/release" folder. When I'm copying this exe file to desktop or other location, and trying to run batch file shows an exception. "System.Diagonostics.Process.StartWithShellExecuteEx(ProecessStartInfo stratinfo) at bt_uninstall_Click(object sender, EventArgs e)" – user2431727 May 10 '19 at 07:21