4

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,

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?

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";
Brian Var
  • 6,029
  • 25
  • 114
  • 212

3 Answers3

7

Try like this

string gesturefile= Path.Combine(Environment.CurrentDirectory, @"GesturePak\wave.xml");

Examples:

http://msdn.microsoft.com/en-us/library/fyy7a5kt(v=vs.110).aspx http://www.dotnetperls.com/path

or

Use Path.GetFullPath

Example : http://msdn.microsoft.com/en-us/library/system.io.path.getfullpath.aspx

  • But you should keep in mind that the value of the `Environment.CurrentDirectory` property may change during the runtime of the application. – Clemens Mar 27 '14 at 11:29
  • 1
    the following worked for me, `private string gesturefile = System.IO.Path.Combine(Environment.CurrentDirectory, @"Gesture\wave.xml");` – Brian Var Mar 27 '14 at 19:45
5

If you do not want to store files separately you need to set their Build Action to Resource (in the Properties window in Visual Studio) and access them by a 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:

string content;
using (StreamReader reader = new StreamReader(resourceStream, Encoding.UTF8))
{
    content = reader.ReadToEnd();
}
Clemens
  • 123,504
  • 12
  • 155
  • 268
  • The code is meant to be executed in a method. Perhaps in the constructor of your MainWindow class. – Clemens Mar 27 '14 at 14:35
  • okay, then how do I convert this `resource stream` back to a string? As it requires a string type. – Brian Var Mar 27 '14 at 14:38
  • You mean you want to read the content of the resource (XML) file? See my edit please. – Clemens Mar 27 '14 at 14:46
  • ,no I just need a string variable to point to the file, I have an api that does the reading.Is there a way to point a string variable to the file path? – Brian Var Mar 27 '14 at 15:20
  • Do you understand what I mean? the file is read by the api so I just need to have a string variable that points to the folder in my project called "Gesture".If I use your above solution the file will be parsed into this variable which doesn't suit. – Brian Var Mar 27 '14 at 16:26
  • Of course I understand that. And you could have mentioned this fact before, ideally in your question. When that API requires a file name, I'm pretty sure that it wants to read a regular file from your local file system. This can't be done with a pack URI, which references a file resource inside your application's assembly. – Clemens Mar 27 '14 at 17:08
  • okay, the gesture file is being added to a list wthin the program like this ` Gesture g1 = new Gesture(gesturefile); // Add it to a gestures collection List gestures = new List(); gestures.Add(g1);` – Brian Var Mar 27 '14 at 19:31
1

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

string address="Images/dukenukem.jpg";
StepUp
  • 36,391
  • 15
  • 88
  • 148
  • 1
    Not without either making the file a resource and using a pack URI or taking care that VS copies the Images folder to the current project's output directory. – Clemens Mar 27 '14 at 11:08