0

I'm working on cook book Windows project, and I prepared some .rtf files to load it in RichTextBox. The problem is that the path of the file can't work at any computer, just mine. The code I used:

richTextBox1.LoadFile("C:\\Users\\nardeen\\Desktop\\cookproject\\cookingproject\\italian.rtf", RichTextBoxStreamType.RichText);

In addition I tried with this but it didn't work:

string FilePath = System.Windows.Forms.Application.StartupPath + "\\italian.rtf";

richTextBox1.LoadFile(FilePath, RichTextBoxStreamType.RichText);
Fedor
  • 1,548
  • 3
  • 28
  • 38
  • 2
    If you want the app EXE path see here https://stackoverflow.com/questions/52797/how-do-i-get-the-path-of-the-assembly-the-code-is-in … otherwise you need the environment variable for the user desktop folder (which looks like your first path). – James Harcourt Jun 03 '18 at 20:29
  • inspect the runtime value of `StartupPath` in the debugger, what does it show? maybe you're one directory too far up, or in the bin folder, so you would need to move one up and then into the "cookingproject" folder. if you are going to publish your application, you should include the text file as an embedded resource, or have the installer put it in a runtime directory with a known relative path. – Cee McSharpface Jun 03 '18 at 20:31
  • when I run the code with the second method it shows like this: An unhandled exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll –  Jun 03 '18 at 20:38

2 Answers2

1

Do not use StartupPath or CurrentDirectory for user data; that will change depending on how your application was installed, Standard users won't be able to write to it, and it will probably get deleted if the user repairs their application.

If you are trying to get the path to a folder that the user can read and write to, and that doesn't get deleted when you uninstall or repair the application, use

string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
    "cooking project", "Italian.rtf");

If you want to include some starter data for users, include it in your project as a resource or embedded file, and copy it to the user folder when you install your application. If you tell us what installer technology you use, we can give advice on that as well.

Dour High Arch
  • 21,513
  • 29
  • 75
  • 90
  • it worked thank you, can you explain to me please what "Environment" means and how it work? –  Jun 04 '18 at 09:01
  • [`System.Environment`](https://learn.microsoft.com/en-us/dotnet/api/system.environment?view=netframework-4.7.1) is a .NET class. Any time you see a class you don't recognize, first look for it in the [API browser](https://learn.microsoft.com/en-us/dotnet/api/index). – Dour High Arch Jun 04 '18 at 13:26
0

You can use System.IO.Directory.GetCurrentDirectory() to get the file path of the folder your .exe is in

Application.StartUpPath includes the actual program.exe in the string (i.e. C:\Program\Program.exe as opposed to C:\Program)

Nolan B.
  • 427
  • 1
  • 4
  • 9