簡體   English   中英

OpenFileDialog 和 UnauthorizedAccessException

[英]OpenFileDialog and UnauthorizedAccessException

我正在開發一種將 .fbx 模型和用戶輸入處理到單個文件中以用於游戲的工具。 用戶按下“導入模型”按鈕時的代碼如下,每個按鈕都類似:

private void E_ImportModelButton_Click_1(object sender, EventArgs e)
{
    E_model = null; // byte array where model is stored
    E_SelectedFileLabel.Text = "No Model Selected"; // label on form
    OpenFileDialog ofd = new OpenFileDialog();
    ofd.Filter = "FBX Model (.fbx)|*.fbx";
    ofd.Multiselect = false;
    if (ofd.ShowDialog() == DialogResult.OK)
    {
        // adjusts variables for game file
        string s = Path.GetDirectoryName(ofd.FileName);
        E_model = File.ReadAllBytes(s);
        E_SelectedFileLabel.Text = "File Selected: " + ofd.FileName;
    }
}

問題是,每當我單擊“確定”時,都會發生UnauthorizedAccessException 我曾嘗試從C:\\Users\\Owner\\Downloads以及C:\\Users\\Owner\\DesktopC:\\驅動器本身導入文件,但它仍然發生。 我可以在此代碼中添加什么來訪問這些(和其他)文件夾?

您正在嘗試通過旨在從文件中讀取的方法從目錄中讀取:

string s = Path.GetDirectoryName(ofd.FileName);
E_model = File.ReadAllBytes(s);

替換為:

E_model = File.ReadAllBytes(ofd.FileName);

你不能准備好目錄,你必須讀取一個文件:

string s = Path.GetDirectoryName(ofd.FileName);
E_model = File.ReadAllBytes(s);

嘗試在此處添加文件名

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM