Access to the path 'C:\Users\xxx\Desktop'

2020-03-08 09:12发布

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.

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);
    }
}

5条回答
我想做一个坏孩纸
2楼-- · 2020-03-08 09:21

"C:\\Users\\username\\Desktop" is a directory for me; not a file.

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);
查看更多
成全新的幸福
3楼-- · 2020-03-08 09:28

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.

查看更多
虎瘦雄心在
4楼-- · 2020-03-08 09:35

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);
查看更多
可以哭但决不认输i
5楼-- · 2020-03-08 09:36

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!

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?

查看更多
女痞
6楼-- · 2020-03-08 09:36
  1. 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)

  2. 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.

  3. 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.

LINK TO SOURCE: enter link description here

查看更多
登录 后发表回答