简体   繁体   中英

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?

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(),"")

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM