0

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");
Ritzel
  • 33
  • 2
  • 4
  • 9

3 Answers3

1

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)

Shimrod
  • 3,115
  • 2
  • 36
  • 56
  • Why would you deploy an application to "My Documents" ...? =) – gdoron Mar 13 '12 at 12:41
  • I need the file path for portable files because I'm putting the User Manual & Database together with the entire source code and exe in a CD. I don't think that would work. – Ritzel Mar 13 '12 at 12:45
  • ohh i get it.. so basically you have a project A ( name it) and now that project has Source Code / Database / User manual which is in pdf. Is that so? – MDMalik Mar 13 '12 at 13:09
0

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);
Steve Danner
  • 21,818
  • 7
  • 41
  • 51
  • Thanks for the quick response Steve! My PDF is under the **User Manual** Folder while the exe is under **obj > Debug** – Ritzel Mar 13 '12 at 12:43
  • 1
    Consider deploying your EXE and your PDF to the same directory then. A typical deployment scenario has your EXE, your dependent DLLs and all support files in a single directory or subdirectories thereof. – Steve Danner Mar 13 '12 at 12:45
  • Would you know how I will be able to integrate [this](http://stackoverflow.com/questions/1423922/open-a-pdf-file-programmatically-at-a-named-destination) with your given code, Steve? – Ritzel Mar 13 '12 at 13:22
  • @Ritzel, that code doesn't seem very robust. I would ask another question of the best practice to find the Adobe EXE path, then take it from there :) – Steve Danner Mar 13 '12 at 15:27
0

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.

MDMalik
  • 3,951
  • 2
  • 25
  • 39
  • Hi, yes I am not deploying with the project that is why I need a path that will be able to read the file in the directory regardless of the drive it is in. The exe will be placed in a CD. – Ritzel Mar 13 '12 at 12:47
  • How are you planning to run the exe from a cd. I know it does but would that be a directly click on exe without installing or anything.? – MDMalik Mar 13 '12 at 12:59