如何指向存储在项目文件夹中的文件?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
How to point to a file stored in project folder? WPF
提问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
Try like this
像这样尝试
string gesturefile= Path.Combine(Environment.CurrentDirectory, @"GesturePak\wave.xml");
Examples:
例子:
http://msdn.microsoft.com/en-us/library/fyy7a5kt(v=vs.110).aspxhttp://www.dotnetperls.com/path
http://msdn.microsoft.com/en-us/library/fyy7a5kt(v=vs.110).aspx http://www.dotnetperls.com/path
or
或者
Use Path.GetFullPath
用 Path.GetFullPath
Example : http://msdn.microsoft.com/en-us/library/system.io.path.getfullpath.aspx
示例:http: //msdn.microsoft.com/en-us/library/system.io.path.getfullpath.aspx
回答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 Action为Resource(在 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";

