Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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);     } } 
like image 236
Sainath Avatar asked Jun 25 '13 17:06

Sainath


People also ask

How do I give access to a file path?

Click the Start button and then click Computer, click to open the location of the desired file, hold down the Shift key and right-click the file. Copy As Path: Click this option to paste the full file path into a document. Properties: Click this option to immediately view the full file path (location).


1 Answers

You may have to run your program/IDE as Administrator to access that folder, due to how Windows default permissions work.

For more context:

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

like image 93
Ricky Mutschlechner Avatar answered Sep 23 '22 01:09

Ricky Mutschlechner