using (var openFileDialog1 = new OpenFileDialog())
{
openFileDialog1.Reset();
if (!string.IsNullOrEmpty(ExcelFilePath))
{
string fileName = Path.GetFileName(ExcelFilePath);
string fileExt = Path.GetExtension(ExcelFilePath);
//Avoid "you can't open this location using this program file" dialog
//if there is a file name in the path strip it)
if (!string.IsNullOrEmpty(fileName))
initialDirectory = Path.GetDirectoryName(ExcelFilePath);
//if not let it be
else
initialDirectory = ExcelFilePath;
openFileDialog1.InitialDirectory = initialDirectory;
}
else
openFileDialog1.InitialDirectory = "c:\\";
openFileDialog1.Filter = "Excel files (*.xls or *.xlsx)|*.xls;*.xlsx";
//openFileDialog1.Filter = "xls files (*.xls)|*.xls|xlsx files(*.xlsx)|.xlsx";
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = false;
openFileDialog1.CheckFileExists = true;
openFileDialog1.CheckPathExists = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
var browseSelectionMade = BrowseSelectionMade;
if (browseSelectionMade!=null)
browseSelectionMade(this, new DataEventArgs<string>(openFileDialog1.FileName));
}
}
無論我是否將RestoreDirectory設置爲true,如果我的初始目錄設置爲不存在的路徑,我將始終瀏覽到LAST使用的目錄。 OpenFileDialog保存的最後一個使用目錄在哪裏?有沒有辦法來覆蓋這種行爲? (例如,如果初始目錄不存在,我總是希望將它設置爲C:\)OpenFileDialog默認路徑
感謝您的答覆,我會將您的答案標記爲已接受的答案,儘管第二個答案也很好。事實上,我最終確定了路徑的存儲位置,這對我來說是很有幫助的。再次感謝! – 2012-04-02 17:26:57
Upvoting _Hans Passant_感覺如此毫無意義...... – itsho 2017-01-08 13:28:12