user9889331
user9889331

Reputation:

How to get file path?

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);

Upvotes: 0

Views: 530

Answers (2)

Dour High Arch
Dour High Arch

Reputation: 21722

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.

Upvotes: 1

Nolan B.
Nolan B.

Reputation: 457

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)

Upvotes: 0

Related Questions