0
private void loadWordsFromFile()
{
    words = File.ReadAllLines("C:/Users/tony/Documents/Visual Studio 2013/Projects/Hangman/Hangman/Files/test.txt");
}

private void selectWord()
{
    WordsRemaining = words.Length.ToString();
    HangImage = new BitmapImage(new Uri("C:/Users/tony/Documents/Visual Studio 2013/Projects/Hangman/Hangman/Files/" + wrongGuesses + ".png"));
}

These are my paths. Could you please show me how to make the paths properly?

coolside
  • 81
  • 9
  • It is not clear what you are asking – user469104 Apr 21 '16 at 13:18
  • Possible duplicate of [Get current folder path](http://stackoverflow.com/questions/15653921/get-current-folder-path) – Regis Portalez Apr 21 '16 at 13:19
  • I'd like not to have the full path but a relative because in this way I cannot publish my app – coolside Apr 21 '16 at 13:20
  • 1
    Create a `Files` folder in your Visual Studio Project and put all necessary files in that folder. In the Properties of each file set `Build Action` to `Content` and `Copy to Output Directory` to `Copy always` or `Copy if newer`. Thus the files will be copied to the output directory and can be accessed by relative paths like `Files/test.txt`. – Clemens Apr 21 '16 at 13:23
  • it works for the loadWordsFromFile function but when I apply the following code for the image: HangImage = new BitmapImage(new Uri("Files/" + wrongGuesses + ".png")); it gives me the following error after startup: An exception of type 'System.UriFormatException' occurred in System.dll but was not handled in user code Additional information: Invalid URI: The format of the URI could not be determined. – coolside Apr 21 '16 at 13:36

2 Answers2

0

You can use this to get the current execution path

    Uri executingPathUri = new Uri(Assembly.GetExecutingAssembly().GetName().CodeBase);

    string executionFolderPath = Path.GetDirectoryName(executingPathUri.LocalPath);

Then, you can have a folder in your deployment directory that contains the files that you need.

Timothy Ghanem
  • 1,606
  • 11
  • 20
0

Please never try to concatenate path using '+' operator. Always use

Path.Combine(.....)

public static string Combine(string path1, string path2, string path3, string path4);
user1386121
  • 91
  • 3
  • 13