用exe打开OpenFileDialog文件夹

时间:2012-12-03 23:11:15

标签: c# openfiledialog

如何使用OpenFileDialog快速打开应用程序的文件夹?

        OpenFileDialog openFileDialog1 = new OpenFileDialog();
        openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
           ...........

        }

2 个答案:

答案 0 :(得分:2)

如果您希望覆盖其值设置的默认方式之一(在MSDN上描述),请使用FileDialog.InitialDirectory Property

openFileDialog1.InitialDirectory = @"C:\";  // based on comment of question

答案 1 :(得分:2)

猜测你的意思是“在应用程序的文件夹中显示OpenFileDialog”,只需将OpenFileDialog.InitialDir设置为应用程序的文件夹,然后再显示OpenFileDialog

string AppPath = Path.GetDirectoryName(Application.ExecutablePath);;
openFileDialog1.InitialDir = AppPath;

如果您在查找应用程序目录时需要帮助,请参阅Getting root folder of application