-1

So this application is going to be an .EXE file and used in other office computers. One problem though. If i would open a file, that specific computer would have another path so my code wouldn't work. Is there a way to specify only the folder? I mean like for instance C:/ApplicationFolder/Hejj.pdf.

I hope you understand my question.

private void Button_Click_8(object sender, RoutedEventArgs e)
{    
    Process.Start(@"C:\Users\Danie\Desktop\Hejj.pdf");      
}
gogaz
  • 2,323
  • 2
  • 23
  • 31
Daniel Saggo
  • 108
  • 1
  • 9

2 Answers2

3

it is supposed to be in the same folder the .exe file is going to be placed

you can use Assembly.GetExecutingAssembly().Location to get the current exe's path

Path.Combine(Assembly.GetExecutingAssembly().Location, "Hejj.pdf") will give you the full path of the pdf file

Steve
  • 11,696
  • 7
  • 43
  • 81
  • @DanielSaggo copy paste my code to replace your hard coded pdf path and you are done – Steve Aug 20 '18 at 17:40
  • {Process.Start(Path.Combine(Assembly.GetExecutingAssembly().Location, "Hejj.pdf")); – Daniel Saggo Aug 20 '18 at 17:44
  • like that or like so Path.Combine(Assembly.GetExecutingAssembly().Location, "Hejj.pdf"); – Daniel Saggo Aug 20 '18 at 17:45
  • 1
    @DanielSaggo you gonna understand how it works. Path.Combine will give you a path. If you want to start a program then you do Process.Start({program path}). – Steve Aug 20 '18 at 18:44
0

You could use the system special folder environment constants like

private void Button_Click_8(object sender, RoutedEventArgs e)
{    
    string path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.DesktopDirectory);
    Process.Start(System.IO.Path.Combine(path, Hejj.pdf));      
}

to get the path to the desktop directory.

GetFolderPath returns the path to the location. The locations of these folders can have different values on different operating systems. See MSDN for all available directory constants.

dontbyteme
  • 1,221
  • 1
  • 11
  • 23