我正在开发一种工具,可以将 .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\Desktop
和 C:\
导入文件> 自行驱动,但仍然发生。我可以向此代码添加什么来获得对这些(和其他)文件夹的访问权限?
最佳答案
您正在尝试通过用于从文件读取的方法从目录中读取数据:
string s = Path.GetDirectoryName(ofd.FileName);
E_model = File.ReadAllBytes(s);
替换为:
E_model = File.ReadAllBytes(ofd.FileName);
关于c# - OpenFileDialog 和 UnauthorizedAccessException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33404313/