我開始在 .NET 5.0 中創建一個簡單的 OpenFileDialog,但System.IO.FileNotFoundException: 'Unable to find the specified file.' 在我什至可以看到瀏覽對話框之前出現
例外。
當我點擊m_FileUploader按鈕時,對話框應該打開。這是我使用的代碼:
Main.Designer.cs
//
// m_FileUploader
//
this.m_FileUploader.Location = new System.Drawing.Point(508, 17);
this.m_FileUploader.Name = "m_FileUploader";
this.m_FileUploader.Size = new System.Drawing.Size(234, 52);
this.m_FileUploader.TabIndex = 0;
this.m_FileUploader.Text = "Browse descr_names.txt";
this.m_FileUploader.UseVisualStyleBackColor = true;
this.m_FileUploader.Click = new System.EventHandler(this.FileUploader_Click);
//
// m_OpenFileDialog
//
this.m_OpenFileDialog.Filter = "Names |*descr_names.txt";
this.m_OpenFileDialog.InitialDirectory = "Documents";
this.m_OpenFileDialog.Title = "Open descr_names.txt";
this.m_OpenFileDialog.RestoreDirectory = true;
主檔案
private void FileUploader_Click (Object sender, EventArgs e) {
if (m_OpenFileDialog.ShowDialog() == DialogResult.OK) { // Exception throws here
// Do stuff with file
}
}
當我在除錯中單擊繼續時,其余代碼正確執行,但獨立可執行檔案不喜歡此例外并停止應用程式。
我在這里忘記了什么?
uj5u.com熱心網友回復:
您應該為 使用不同的值InitialDirectory,因為值Documents可能無法正確處理。相反,你應該這樣做:
using System;
// Get path for User's documents folder from .NET
var pathToDocs = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments));
this.m_OpenFileDialog.InitialDirectory = pathToDocs;
有關更多詳細資訊,請參閱檔案SpecialFolder。或InitialDirectory檔案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/360413.html
下一篇:標簽未在文本輸入上方的左上角對齊
