简体   繁体   中英

How do you pack any file with WPF application?

I am trying to pack a certain file into my project. The file is a stl file [3d model], how can I pack files like these into my WPF app. So if I reference them somewhere in my code I can use them. For example if my file is at:

C:\Users\user\Downloads\myProjectFolder\myFolder\my3dFile.stl

I want to pack that file with my WPF app. And I also need a way to reference it when I need it. How can I do this?

There's a number of different ways to do this, one of the easiest is to simply add it to your project and set it as an Embedded Resource in it's Properties tab. Then you can load it with something like this:

protected byte[] GetResource(string resource)
{
    var assembly = System.Reflection.Assembly.GetExecutingAssembly();
    using (var stream = assembly.GetManifestResourceStream(resource))
    {
        byte[] buffer = new byte[stream.Length];
        stream.Read(buffer, 0, buffer.Length);
        return buffer;
    }
}

...which you would then call like so:

var fileData = GetResource("MyProject.MyFolder.my3dFile.stl");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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