Reading an embedded text file

后端 未结 3 2359
忘了有多久
忘了有多久 2021-02-20 13:07

I have created a full project which works perfectly. My problem concerns the setup project. When I use it on another computer, the text file cannot be found even if they are ins

3条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-20 13:51

    Assuming you mean that you have a file in your project that you've set as an EmbeddedResource, you want

    using (var stream = Assembly.GetExecutingAssembly()
        .GetManifestResourceStream(path))
    {
        ...
    }
    

    where path should be the assembly name followed by the relative path to your file in the project folder hierarchy. The separator character used is the period ..

    So if you have an assembly called MyCompany.MyProject and then in that project you have a folder Test containing Image.jpg, you would use the path MyCompany.MyProject.Test.Image.jpg to get a Stream for it.

提交回复
热议问题