Ritzel
Ritzel

Reputation: 33

How to properly write file path for portable files?

I developed a desktop system and it needs to be deployed. My paths are in full. See example below. I am worried that when I deploy my system, it would not run in their computers because they do not have the D drive or the MY_THESIS folder. Help?

System.Diagnostics.Process.Start(@"D:\MY_THESIS\WORKING FILES\WindowsFormsApplication2\WindowsFormsApplication2\User Manual\User Guide Outline.pdf");

Upvotes: 0

Views: 1362

Answers (3)

MDMalik
MDMalik

Reputation: 4001

Use app.config file There

connectionStrings>
   <add name="File Path" connectionString="D:\MY_THESIS\WORKING FILES\WindowsFormsApplication2\WindowsFormsApplication2\User Manual\User Guide Outline.pdf"
          />
</connectionStrings>

Now once you deploy on that machine you can just chance the connection string xml file to which ever path the file is situated. So that you do get that file. If you are not deploying with the project.

Upvotes: 0

Steve Danner
Steve Danner

Reputation: 22148

Assuming your exe is in the same directory as the PDF, simply use this:

string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);

path = System.IO.Path.Combine(path, "User Guide Outline.pdf");
System.Diagnostics.Process.Start(path);

Upvotes: 0

Shimrod
Shimrod

Reputation: 3205

The best practice would be to write in a directory you are sure will exist, such as My Documents.

This snippet gives you access to this directory :

Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)

The SpecialFolder enum gives you access to other common directories.

Otherwise, if you are the one who chooses where the pdf will be places, you can always place the PDF in the application directory and access it using Assembly.GetExecutingAssembly().Location or System.IO.Path.GetDirectoryName(Application.ExecutablePath)

Upvotes: 1

Related Questions