简体   繁体   English

我可以将 windows (.bat ) 文件放在 Visual Studio C# 项目的资源文件夹中吗?

[英]Can I put a windows (.bat ) file inside the resources folder of a Visual studio C# project?

Although one can run a batch file form c: location, i would like to know if its possible to have the.bat file inside the resources folder.虽然可以从 c: 位置运行批处理文件,但我想知道是否可以在资源文件夹中包含 .bat 文件。

I tried this我试过这个

Process p = new Process;
p.StartInfo.FileName = @"\Resources\batchfile.bat";

and this和这个

p.StartInfo.FileName = @"\Resources\batchfile";

Both don't work.两者都不起作用。

You can put a batch file in whatever file-system location you want.您可以将批处理文件放在您想要的任何文件系统位置。

It looks like the path to your batch file is wrong, though.不过,看起来批处理文件的路径是错误的。 Paths with a leading backslash are interpreted relative to the root directory of the current drive.带有前导反斜杠的路径相对于当前驱动器的根目录进行解释。 That's probably not where your batch file is, though.不过,这可能不是您的批处理文件所在的位置。 It's probably in the Resources subdirectory of your application's own installation folder.它可能位于应用程序自己的安装文件夹的 Resources 子目录中。 At the very least, remove the leading backslashes from those strings.至少,从这些字符串中删除前导反斜杠。 Then they will be interpreted relative to your process's current working directory.然后它们将相对于您的进程的当前工作目录进行解释。

It would be better to use a fully qualified path, though.不过,最好使用完全限定的路径。 The current working directory has a tendency to change when you're not expecting.当前工作目录往往会在您意料之外的情况下发生变化。

You would want to include the.bat file as an embedded resource.您可能希望将 .bat 文件作为嵌入式资源包含在内。 So in Visual Studio you would open up the Properties on the file, and select "Embedded Resource" for the "Build Action".因此,在 Visual Studio 中,您将打开文件的属性,并为“构建操作”打开 select“嵌入式资源”。

Now for the fun part....in your app you'll want to extract the file and write it to disk prior to executing using the GetManifestResourceStream method on the Assembly object.现在是有趣的部分......在您的应用程序中,您需要在使用程序集 object 上的GetManifestResourceStream方法执行之前提取文件并将其写入磁盘。 This is slightly tricky because you need to pass a resource name to the method, and that name ends up being based on your assemblies namespace, plus the path (so if your project is MyProject and your file in in Resources\MyBat.bat then the resource name would be MyProject.Resources.MyBat.bat ...at least I think thats right)这有点棘手,因为您需要将资源名称传递给方法,并且该名称最终基于您的程序集命名空间以及路径(因此,如果您的项目是MyProject并且您的文件位于Resources\MyBat.bat中,那么资源名称将是MyProject.Resources.MyBat.bat ...至少我认为这是正确的)

There is actually an existing SO question about how to do this here , and has a much nicer code sample than the one I was going to whip up.实际上存在一个关于如何在此处执行此操作的 SO 问题,并且有一个比我要编写的代码示例更好的代码示例。 :) :)

string location;

Process p = new Process;

location = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location)
+@"\Resources\batchfile.bat";

p.StartInfo.FileName = location;

Resources (of your C# project) are typically files linked into your assembly at compile time.资源(您的 C# 项目)通常是在编译时链接到您的程序集的文件。 If you put a batch file there, you have to extract it at run-time to a folder like the %TEMP% folder first and run it from there.如果您将批处理文件放在那里,则必须在运行时将其解压缩到 %TEMP% 文件夹之类的文件夹中,然后从那里运行它。

暂无
暂无

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

相关问题 将子文件夹放入Resources文件夹Visual Studio C #Windows窗体 - Having a subfolder into Resources folder Visual Studio C# Windows Form 如何在Visual Studio中使用C#在解决方案中轻松获取文件夹内文件的路径? - How can I easily get the path of a file inside a folder in my solution in Visual Studio with C#? 如何打开“测试项目”代码文件夹中的文件? (Visual Studio 2008和.NET C#) - How to open a file inside a Test Project code folder? (Visual Studio 2008 and .NET C#) 如何在Visual Studio项目C#中引用包含的文件 - How to refer to included file inside a visual studio project c# 在Visual Studio中,如何从项目Resources文件夹中加载.png图像? - In Visual Studio how can I load a .png image from the project Resources folder? 我想访问visual studio命令propmt并使用C#代码运行.bat文件 - I want to access visual studio command propmt and run a .bat file using C# code 如何在没有默认 DLL 文件的情况下编译 C# Visual Studio 项目? - How can I compile a C# visual studio project without the default DLL file? 我无法在 Visual Studio 中执行软件项目,C# - I can't excute software project in visual studio, C# 如何在C#Windows Service项目中启动.bat文件 - How to launch a .bat file in a C# windows Service project 如何在Windows窗体中将.png文件用作按钮? (在C#中 - visual studio 2013) - How can I use a .png file as a button in Windows Form ? ( in C# - visual studio 2013 )
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM