Using OpenFileDialog w/ a relative path as initialDirectory

折月煮酒 提交于 2019-12-24 04:34:06

问题


I'd like to give the appropriate value to Initial directory, so it would open the folder (called "Images") which I created in my project. I must use a relative path, so my program works not depending on the computer I work . But the problem is I don't know how to access this folder...

Does anyone know how to solve this problem?


回答1:


use:

openFileDialog.InitialDirectory = Path.Combine(Application.StartupPath,
@"YourSubDirectoryName");

Edit: or try this if you prefer... are you in windows forms?

openFileDialog.InitialDirectory = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(Application.ExecutablePath), @"YourSubDirectoryName");

Edit 2 for WPF:

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

... from the Microsoft Forums...



来源:https://stackoverflow.com/questions/7416605/using-openfiledialog-w-a-relative-path-as-initialdirectory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!