开发者

Get Relative Path to Linked Resource in C#

开发者 https://www.devze.com 2022-12-24 04:01 出处:网络
I have a number of linked resources (text files in this case) in a C# project I am working on.I know the resources are linked via relative path in the Resources file.Is there any way to开发者_如何学运

I have a number of linked resources (text files in this case) in a C# project I am working on. I know the resources are linked via relative path in the Resources file. Is there any way to开发者_如何学运维 get that relative path from within the C# code?

If there isn't, what I am trying to do is to pass that resources file to an external application as a commandline argument. If I can't do it the first way (which would obviously be the easiest) any other suggestions would be much appreciated. Thanks!


After compilation the resource files are part of a dll. So they do not exist anymore in the file system you distribute. If you need to have the resource files as a ressource then Hueso's solution is the only way of passing the files on to an external application.

However, if you don't mind the files being accessible by the user you can set the BuildAction to Content and set the Copy to Output Directory to either Copy if newer or Copy always. The files are then copied into the output directory and can be accessed from there. If the files are in sub folders these subfolders are also copied into the output directory.

Edit:

For the various build actions and their effect see:

  • What are the various "Build action" settings in Visual Studio project properties and what do they do?


I think you can get the resource, save it to a temporary folder and then supply the path to your command line external application, this is a quick and dirty way, but that's the main idea:

var assembly = System.Reflection.Assembly.GetExecutingAssembly();
var stream = assembly.GetManifestResourceStream("Resources.yourFile.txt");
var tempPath = Path.GetTempPath();
File.Save(stream, tempPath);

// use your tempPath here


These three lines takes your resource file and output to byte array, then write it to a file on disk.

byte[] byteArray = new byte[Project.Properties.Resources.File.Length];
Project.Properties.Resources.File.CopyTo(byteArray, 0);

File.WriteAllBytes("Filepath", byteArray);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号