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

前端 未结 6 1567

For example i have this code:

Process proc = new Process();
proc.EnableRaisingEvents = true;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.FileName          


        
相关标签:
6条回答
  • 2020-12-12 06:36

    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".

    0 讨论(0)
  • 2020-12-12 06:42

    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

    0 讨论(0)
  • 2020-12-12 06:47
    // 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");
    
    0 讨论(0)
  • 2020-12-12 06:49

    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.

    0 讨论(0)
  • 2020-12-12 06:54

    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.

    0 讨论(0)
  • 2020-12-12 06:54

    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.

    0 讨论(0)
提交回复
热议问题