X-Plore-It
X-Plore-It

Reputation: 33

Get relative path from OpenFileDialog in C#

I am a beginner at programing and I´m writing my second Prog. I have a Question about how to get a relative path to my application Startup path. The Program reads an .xml-file that has a path of a .jpg stored in it. It creates a Picturebox for every path and loads the respective image. The problem I have is, that I have the images in my Dropbox to be able to use the Program on any PC that has Dropbox. When I use the OpenFileDialog on my main PC and save the Path of the .jpg to the xml it won´t work on my laptop because the Dropbox folder is on another drive as on my main PC.

Does anyone have an idea, how to get around this Problem?

Upvotes: 1

Views: 5008

Answers (2)

Robin V.
Robin V.

Reputation: 1542

When the dropbox folder is in the default location (User-folder) you can use this to get your path:

string userFolderPath= Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);

This will give C:\Users\USERNAME (drive may be different). Then simply add the rest of the path to your image folder.

string imageFolderPath = userFolderPath + @"\Dropbox\Imagefolder";

Upvotes: 0

Riaan Walters
Riaan Walters

Reputation: 2676

To solve your issue, This will get the current location of your application

Directory.GetCurrentDirectory()

You can do a simple replace of the path.

Example :

String JPG_Path_Relative = openFileDialog1.FileName.Replace(Directory.GetCurrentDirectory(),"")

Upvotes: 2

Related Questions