使用OpenFileDialog和一个相对路径作为初始化目录

本文关键字:路径 初始化 相对 一个 OpenFileDialog 使用 | 更新日期: 2023-09-27 18:08:29

我想给Initial directory适当的值,这样它就会打开我在项目中创建的文件夹(称为"Images")。我必须使用一个相对路径,所以我的程序工作不依赖于我工作的计算机。但问题是我不知道如何访问这个文件夹…

有谁知道如何解决这个问题吗?

使用OpenFileDialog和一个相对路径作为初始化目录

use:

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

编辑:或者如果你喜欢的话试试这个…你使用的是Windows窗体吗?

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

编辑2:

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