[英]OpenFileDialog and UnauthorizedAccessException
I'm developing a tool that processes an .fbx model and user input into a single file for use in a game.我正在开发一种将 .fbx 模型和用户输入处理到单个文件中以用于游戏的工具。 The code for when the user presses the "Import Model" button is as follows, and is similar for every button:
用户按下“导入模型”按钮时的代码如下,每个按钮都类似:
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;
}
}
The problem is, whenever I click OK, an UnauthorizedAccessException
occurs.问题是,每当我单击“确定”时,都会发生
UnauthorizedAccessException
。 I have tried importing files from C:\\Users\\Owner\\Downloads
as well as C:\\Users\\Owner\\Desktop
and the C:\\
drive itself, but it still occurs.我曾尝试从
C:\\Users\\Owner\\Downloads
以及C:\\Users\\Owner\\Desktop
和C:\\
驱动器本身导入文件,但它仍然发生。 What could I add to this code to gain access to these (and other) folders?我可以在此代码中添加什么来访问这些(和其他)文件夹?
You are trying to read from directory via the method intended to read from a file:您正在尝试通过旨在从文件中读取的方法从目录中读取:
string s = Path.GetDirectoryName(ofd.FileName);
E_model = File.ReadAllBytes(s);
Replace it with:替换为:
E_model = File.ReadAllBytes(ofd.FileName);
You can't ready the directory, you have to read a file:你不能准备好目录,你必须读取一个文件:
string s = Path.GetDirectoryName(ofd.FileName);
E_model = File.ReadAllBytes(s);
Try adding the file name here尝试在此处添加文件名
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.