C# 拒绝访问路径“C:\Users\xxx\Desktop”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17304191/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Access to the path 'C:\Users\xxx\Desktop' is denied
提问by Sainath
I have thoroughly searched the entire access denied questions and did't find any question related to access to windows form on my own system all the questions are related to web app.
我已经彻底搜索了整个拒绝访问的问题,但没有发现任何与在我自己的系统上访问 Windows 表单相关的问题,所有问题都与 Web 应用程序有关。
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
byte[] imgdata;
FileStream fsrw;
string fname;
openFileDialog1.Filter = "Sai Files(*.JPG;*.GIF)|*.jpg;*.gif|All files (*.*)|*.*";
openFileDialog1.ShowDialog();//opens the dialog box
fname = openFileDialog1.FileName;//stores the file name in fname
pictureBox1.ImageLocation = fname;//gives the image location to picturebox
fsrw = new FileStream("C:\Users\Sainath\Desktop", FileMode.Open, FileAccess.ReadWrite);
imgdata = new byte[fsrw.Length];
fsrw.Read(imgdata, 0, Convert.ToInt32(fsrw.Length));
fsrw.Close();
string s = "insert into imagetest values(@p1,@p2)";
SqlConnection con = new SqlConnection("server=.;Data Source=.;Initial Catalog=Work;Integrated Security=True");
SqlCommand cmd = new SqlCommand(s, con);
cmd.Parameters.AddWithValue("@p1", imgdata);
cmd.Parameters.AddWithValue("@p2", fname);
con.Open();
int i = cmd.ExecuteNonQuery();
con.Close();
Console.WriteLine(i);
}
}
采纳答案by Ricky Mutschlechner
You may have to run your program/IDE as Administrator to access that folder. I'm not exactly sure why, but I've had the same problem. Something to do with default Windows permissions. Let us know if it works!
您可能必须以管理员身份运行您的程序/IDE 才能访问该文件夹。我不完全确定为什么,但我遇到了同样的问题。与默认的 Windows 权限有关。让我们知道它是否有效!
Edit:
编辑:
The path leads to a folder - not a file. I believe FileStreams in C-based languages must actually point to a file, rather than a directory: ie. C:\Users\Username\Desktop\file.extension. Can you try this and let us know if it helps at all?
该路径通向一个文件夹 - 而不是一个文件。我相信基于 C 的语言中的 FileStreams 实际上必须指向一个文件,而不是一个目录:即。C:\Users\Username\Desktop\file.extension. 你能试试这个,让我们知道它是否有帮助吗?
回答by Steve
Probably you don't realize that you are trying to open the Desktop folder and then trying to use it as a file.
您可能没有意识到您正在尝试打开桌面文件夹,然后尝试将其用作文件。
If your intent is to write the bytes of the image to your database then your code should be
如果您的意图是将图像的字节写入数据库,那么您的代码应该是
fsrw = new FileStream(fname , FileMode.Open, FileAccess.ReadWrite);
回答by Austin Salonen
"C:\\Users\\username\\Desktop"is a directory for me; not a file.
"C:\\Users\\username\\Desktop"对我来说是一个目录;不是文件。
Since you're attempting to open the file, this:
由于您正在尝试打开文件,因此:
fsrw = new FileStream("C:\Users\Sainath\Desktop", FileMode.Open, FileAccess.ReadWrite);
... should be
... 应该
var fullpath = Path.Combine("C:\Users\Sainath\Desktop", fname);
fsrw = new FileStream(fullpath, FileMode.Open, FileAccess.ReadWrite);
回答by LeRoy
Make sure to use a fully qualified name, including the file name for both the destination and the source. (e.g. C:\Source\file.ext, C:\Destination\file.ext)
Visual Studio should run with the same access rights as the folders you are trying to access. Trying to access something like "My documents" and other locations that you don't need elevated rights to access shouldn't require you to elevate Visual Studio.
You should not have to "acquire" or change the permissions on files and folders that you can normally access from the same user that you are running VS in.
确保使用完全限定的名称,包括目标和源的文件名。(例如 C:\Source\file.ext、C:\Destination\file.ext)
Visual Studio 应以与您尝试访问的文件夹相同的访问权限运行。尝试访问诸如“我的文档”之类的内容以及您不需要提升访问权限的其他位置不应要求您提升 Visual Studio。
您不必“获取”或更改通常可以从运行 VS 的同一用户访问的文件和文件夹的权限。
LINK TO SOURCE: enter link description here
链接到源: 在此处输入链接描述
回答by davidlbaileysr
I found that a file's read only flag (when set on) will prevent FileStream and MemoryMappedFile objects from opening and reading the file. There are two solutions: Untick the read only or change the FileStream/MemoryMappedFile to open in FileMode.Read/MemoryMappedFileAccess.Read; the default read/write behavior for a FileStream is Read/Write.
我发现文件的只读标志(当设置为打开时)会阻止 FileStream 和 MemoryMappedFile 对象打开和读取文件。有两种解决方案: 取消只读或者将FileStream/MemoryMappedFile改为在FileMode.Read/MemoryMappedFileAccess.Read中打开;FileStream 的默认读/写行为是读/写。

