简体   繁体   English

如何将一些bat文件添加到我的项目中,这样我就不再需要硬盘上的文件了?

[英]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: 我是否从我的项目\\ debug目录中删除文件dxdiag.bat进行了测试,以免出现异常:

proc.Start();

That the file is not found. 找不到该文件。 Once the file is in the \\debug directory its working. 一旦文件位于\\ debug目录中,它的工作即可。

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. 我想将dxdiag.bat文件添加到我的项目中,因此,如果我将其发送给其他人,他将能够运行我的程序,并且该进程将从其自身的程序中运行dxdiag.bat,因此我发送该程序的那个不会需要蝙蝠文件在他自己的硬盘上。

As long as you have no variables or nonsingleline-commands you can run this function with each line of the bat-file 只要您没有变量或非单行命令,就可以在bat文件的每一行中运行此函数

    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. 否则,我将创建一个临时的bat文件,运行并删除它。

What you want to do is embed the .bat file as an embedded resource as coshmos suggested. 您要做的是按照coshmos的建议将.bat文件嵌入为嵌入式资源。 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". 为此,您需要在解决方案资源管理器中右键单击.bat文件,选择属性,然后在“构建操作”部分下选择“嵌入资源”。 After that, assuming your bat file is in the program's root directory, the following code should work: 之后,假设您的bat文件位于程序的根目录中,则以下代码应工作:

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. 此代码的前半部分将bat文件从嵌入式资源中拉出,并将其保存为程序根目录中的实际但临时的bat文件。 After that it is basically the same as your code. 之后,它基本上与您的代码相同。

Additionally if you add File.Delete(batFileName); 此外,如果添加File.Delete(batFileName); after this code, it will automatically delete the temporary bat file it created. 此代码之后,它将自动删除它创建的临时bat文件。

// 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. 最简单的解决方案是将bat文件添加到Visual Studio中的项目。 To do this do the next steps: 为此,请执行以下步骤:

  1. Copy the bat file to the directory that holds the project. 将bat文件复制到保存项目的目录。
  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. 当然,这不会阻止用户表单删除bat文件。

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. 右键单击项目,将dxdiag.bat添加到您的项目中,然后选择添加现有项。 Then, once it is in your project, right-click the dxdiag.bat file and select properties. 然后,将其放入您的项目后,右键单击dxdiag.bat文件并选择属性。 Set the "copy to output directory" property to "always". 将“复制到输出目录”属性设置为“始终”。

You can embed your .bat files. 您可以嵌入.bat文件。 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. 如果否,则将它们从嵌入式资源复制到%appdata%\\ YourAppName,然后使用。

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 Microsoft嵌入式资源的说明: http : //support.microsoft.com/kb/319292

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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