简体   繁体   中英

How can i add some bat files to my project so i will not need them anymore on the hard disk?

For example i have this code:

Process proc = new Process();
proc.EnableRaisingEvents = true;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.FileName = "dxdiag.bat";
proc.StartInfo.CreateNoWindow = true;
proc.Start();
proc.WaitForExit();
proc.Close();

I did a test if i delete the file dxdiag.bat from my project \\debug directory im getting an exception on the line:

proc.Start();

That the file is not found. Once the file is in the \\debug directory its working.

I want to add the dxdiag.bat file to my project so if i send to someone else my program he will be able run my program and the process will run the dxdiag.bat from the program it self so the one i sent the program wont need the bat file on his own hard disk.

As long as you have no variables or nonsingleline-commands you can run this function with each line of the bat-file

    internal static void executeCommand(string command, bool waitForExit,
    bool hideWindow, bool runAsAdministrator)
    {
        System.Diagnostics.ProcessStartInfo psi =
        new System.Diagnostics.ProcessStartInfo("cmd", "/C " + command);

        if (hideWindow)
        {
            psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
        }

        if (runAsAdministrator)
        {
            psi.Verb = "runas";
        }

        if (waitForExit)
        {
            System.Diagnostics.Process.Start(psi).WaitForExit();
        }
        else
        {
            System.Diagnostics.Process.Start(psi);
        }
    }

so you can also save the text in the code

string bat_file = @"@echo off\ncopy c:\users\Desktop\filea.txt c:\users\Desktop\fileb.txt\n...";

otherwise i would create a temporary bat file, run it and delete it.

What you want to do is embed the .bat file as an embedded resource as coshmos suggested. To do this you need to right click on the .bat file in the solution explorer, select properties, and then under "Build Action" section select "Embedded Resource". After that, assuming your bat file is in the program's root directory, the following code should work:

string batFileName = "dxdiag.bat";
string programName = Assembly.GetExecutingAssembly().GetName().Name;

using (Stream input = Assembly.GetExecutingAssembly().GetManifestResourceStream(programName + "." + batFileName))
{
    using (TextReader tr = new StreamReader(input))
    {
        File.WriteAllText(batFileName, tr.ReadToEnd());
    }
}


Process proc = new Process();
proc.EnableRaisingEvents = true;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.FileName = batFileName;
proc.StartInfo.CreateNoWindow = true;
proc.Start();
proc.WaitForExit();
proc.Close();

The first half of this code pulls the bat file out of the embedded resources and saves it as an actual, but temporary, bat file in the programs root directory. After that it is basically the same as your code.

Additionally if you add File.Delete(batFileName); after this code, it will automatically delete the temporary bat file it created.

// create the dxdiag.bat file 
using (StreamWriter sw = new StreamWriter("dxdiag.bat"))
{
   sw.WriteLine(".........");
   // ......
}

Process proc = new Process();
proc.EnableRaisingEvents = true;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.FileName = "dxdiag.bat";
proc.StartInfo.CreateNoWindow = true;
proc.Start();
proc.WaitForExit();
proc.Close();

//delete the file 
File.Delete("dxdiag.bat");

The easiest solution is to add the bat file to the project in Visual Studio. To do this do the next steps:

  1. Copy the bat file to the directory that holds the project.
  2. Add the file to the project.
  3. Set the property "Build Action" to "Content".
  4. Set the property "Copy to output directory" to "Copy Always".

This will copy the bat fill to the output directory of the build. If you then distribute your program you can send the output directory. This will not prevent the user form deleting the bat file of course.

The other option would be to embed the batch file as a resource in your project and use it from there.

Add dxdiag.bat to your project by right-clicking on the project and select add existing item. Then, once it is in your project, right-click the dxdiag.bat file and select properties. Set the "copy to output directory" property to "always".

You can embed your .bat files. When your need them, you check that these files are existed. If no, you copy them from embedded resources to %appdata%\\YourAppName and then use.

How to read embedded resources: How to read embedded resource text file
Description of embedded resources from the Microsoft: http://support.microsoft.com/kb/319292

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM