如何指向存储在项目文件夹中的文件?WPF

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22684594/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 11:22:11  来源:igfitidea点击:

How to point to a file stored in project folder? WPF

c#wpffilereference

提问by Brian J

At the moment I'm pointing to a file on my machine, but I'm wondering how I would point the string to a folder I created within the project. For example the path to the folder in visual studio is,

目前我指向我机器上的一个文件,但我想知道如何将字符串指向我在项目中创建的文件夹。例如,visual studio 中文件夹的路径是,

C:\Users\Drian Darley\documents\visual studio 2013\Projects\KinectBVversion1\KinectKickboxingBVversion1\Gesture\jabtwo.xml 

I have heard of pack URI's, would that suit in this situation or how would I set one up?

我听说过包 URI,在这种情况下是否适合,或者我将如何设置?

This is how it is pointing to the file at present, which is not ideal as it has to point to a file stored locally on the machine.

这就是它目前指向文件的方式,这并不理想,因为它必须指向本地存储在机器上的文件。

private string gesturefile =
            Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\GesturePak\wave.xml";

回答by NullReferenceException

回答by Clemens

If you do not want to store files separately you need to set their Build Actionto Resource(in the Properties window in Visual Studio) and access them by a Pack URI:

如果不想单独存储文件,则需要将它们设置Build ActionResource(在 Visual Studio 的“属性”窗口中)并通过 Pack URI 访问它们:

var uri = new Uri("pack://application:,,,/GesturePAK/wave.xml");
var resourceStream = Application.GetResourceStream(uri).Stream;

Now you can access the content of the XML file by resourceStream. For example reading the file content into a string could be done like this:

现在您可以通过 访问 XML 文件的内容resourceStream。例如,可以像这样将文件内容读入字符串:

string content;
using (StreamReader reader = new StreamReader(resourceStream, Encoding.UTF8))
{
    content = reader.ReadToEnd();
}

回答by StepUp

for example, if you create a folder called "Images" within your project, then you can point like:

例如,如果您在项目中创建一个名为“Images”的文件夹,那么您可以指向:

string address="Images/dukenukem.jpg";