简体   繁体   English

我如何在 my.exe 中包含批处理文件,而不是在 the.exe 所在的文件夹中需要它们

[英]How do i include batch files inside of my .exe Instead of needing them in the folder the .exe is located in

So been searching or the web but can't seem to find an answer that has helped me.所以一直在搜索或 web 但似乎找不到对我有帮助的答案。 I have been looking for almost a week now.我已经找了将近一个星期了。

I created a program in vs, alongside with some batch files.我在 vs 中创建了一个程序,以及一些批处理文件。 The Batch files run great by themselves and through the debug/release when including them in the folder with the.exe.当将批处理文件包含在带有 .exe 的文件夹中时,它们可以自行运行,并且通过调试/发布运行良好。

My problem is I want to be able to ONLY have the.exe file from my release and it still work.我的问题是我希望只能从我的版本中获得 .exe 文件,它仍然可以工作。

Is there a way i can build these files inside the.exe?有没有办法可以在.exe 中构建这些文件? I have tried using c# to write my console commands instead of including seperate batch files.我尝试使用 c# 来编写我的控制台命令,而不是包含单独的批处理文件。 But im pretty new to c# and i get nothing but errors with the commands i want to run/if i run to many lines.但是我对 c# 很陌生,除了我想运行的命令/如果我运行多行时,我什么也得不到。

I would much rather have just c# instead of including the batch files but that I can't seem to figure out a solution to either.我宁愿只拥有 c# 而不是包含批处理文件,但我似乎无法找到解决方案。

Any help would be appreciated.任何帮助,将不胜感激。

This is me currently calling batch files which works just fine.这是我目前调用的批处理文件,效果很好。 Again, if there is a way to just write this in c# instead of calling a batch file I would be happy to learn.同样,如果有一种方法可以在 c# 中编写而不是调用批处理文件,我会很乐意学习。

  Process process = new Process();
            ProcessStartInfo psi = new ProcessStartInfo();
            psi.CreateNoWindow = false;
            psi.Verb = "runas";
            psi.FileName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"/" + "undo.bat";
            psi.UseShellExecute = true;
            process.StartInfo = psi;
            _ = process.Start();
            process.WaitForExit();

I'm starting CyberSecurity soon and am playing around with some Security stuff on my computer.我很快就会开始 CyberSecurity 并在我的电脑上玩一些安全的东西。 Below is a sample code from my batch file to enable Security Protocols.下面是我的批处理文件中用于启用安全协议的示例代码。 If anything how would i write this in c#?如果有的话,我将如何在 c# 中写这个?

echo ...
echo Enabling Windows Firewall

netsh advfirewall set allprofiles state on
echo Enalbing HyperVisor

bcdedit /set hypervisorlaunchtype auto

echo Enabling UAC

%windir%\System32\reg.exe ADD HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System /v EnableLUA /t REG_DWORD /d 1 /f
echo.
echo.
echo Your Computer will now be restarting for changes to take effect! 

timeout 10
shutdown /r /t 001

What you can do is include the batchfiles as embedded resources in your project.您可以做的是将批处理文件作为嵌入资源包含在您的项目中。 Then read them and then execute them.然后阅读它们,然后执行它们。

to include them as embedded resources example...将它们作为嵌入式资源示例...

  1. add them to your project.将它们添加到您的项目中。
  2. right click and go to properties右键单击并 go 到属性
  3. select embedded resource select 嵌入式资源

then to extract...然后提取...

Write file from assembly resource stream to disk 将程序集资源 stream 中的文件写入磁盘

you can then write the file to disk and create process on it.然后,您可以将文件写入磁盘并在其上创建进程。 or there is a way to execute cmd.exe without writing the file to disk but this is a little complicated so the best way is to just write to disk.或者有一种方法可以在不将文件写入磁盘的情况下执行 cmd.exe,但这有点复杂,所以最好的方法是直接写入磁盘。

Execute BATCH script in a programs memory 在程序 memory 中执行 BATCH 脚本

I followed the guide given above and a few others to get my solution to work.我按照上面给出的指南和其他一些指南来让我的解决方案发挥作用。 Embed the resource that's in your solution, then I used the following code to pretty much create the functions of being able to write it.嵌入解决方案中的资源,然后我使用以下代码几乎创建了能够编写它的功能。

private static void Extract(string nameSpace, string outDirectory, string internalFilePath, string resourceName) 
        {

            Assembly assembly = Assembly.GetCallingAssembly();

            using (Stream s = assembly.GetManifestResourceStream(nameSpace + "." + (internalFilePath == "" ? "" : internalFilePath + ".") + resourceName)) 
                using (BinaryReader r = new BinaryReader(s))
                    using (FileStream fs = new FileStream(outDirectory + "//" + resourceName, FileMode.OpenOrCreate))
                        using (BinaryWriter w = new BinaryWriter(fs))
                            w.Write(r.ReadBytes((int)s.Length));

        }

Here is what I used to save, execute then delete the file.这是我用来保存,执行然后删除文件的内容。

    Extract("nameSpace", "outDirectory", "internalFilePath", "resourceName");
    Process process = new Process();
    ProcessStartInfo psi = new ProcessStartInfo();
    psi.CreateNoWindow = false;
    psi.Verb = "runas";
    psi.FileName = @"C:/" + "resourceName";
    psi.UseShellExecute = true;
    process.StartInfo = psi;
    _ = process.Start();
    process.WaitForExit();
    System.Threading.Thread.Sleep(10);

    if ((System.IO.File.Exists(psi.FileName)))
    {
        System.IO.File.Delete(psi.FileName);
    }

Keep in mind im new when it comes to this so im sure there is a better way of writing it, but this worked for me!请记住我是新手,所以我确信有更好的编写方法,但这对我有用!

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

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