【发布时间】:2021-03-23 16:58:58
【问题描述】:
它说文件的路径被拒绝。 找了一个小时没有真正的答案。 请帮忙。
private void btnSetText_Click(object sender, EventArgs e)
{
using (OpenFileDialog dlg = new OpenFileDialog())
{
dlg.InitialDirectory = Application.StartupPath;
dlg.Filter = "Text Document(*.txt)|*.txt|All Files(*.*)|*.*"; //https://stackoverflow.com/questions/48151581/system-argumentexception-filter-string-not-valid
if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
txtSetText.Text = dlg.FileName;
}
}
System.IO.File.WriteAllText(txtSetText.Text , text);
一切都很好并且有效,但是就在这条线上:
System.IO.File.WriteAllText(txtSetText.Text , text);
我不断访问路径 xyz 被拒绝。如何使其可访问?
【问题讨论】:
-
您可以使用文件浏览器访问吗?是否提示您使用管理员权限?在这种情况下,您必须使用提升的权限:Request Windows Vista UAC elevation if path is protected & Getting Elevated Privileges on Demand using C# (Codeproject)。否则,如果您对该文件没有权限,则需要检查/更改permissions。
-
那么如果文件被另一个进程锁定了,你能确认一下可能将问题标记为重复的情况吗?
标签: c# exception visual-studio-2019