Access to the path is denied
-
Hello everyone This is a program for downloading a file from a url when i download a file i got a error like access to the path is denined can any one tell me why this happen This is my code.
public static void Download(String strURLFileandPath, String strFileSaveFileandPath) { HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(strURLFileandPath); HttpWebResponse ws = (HttpWebResponse)wr.GetResponse(); Stream str = ws.GetResponseStream(); byte\[\] inBuf = new byte\[100000\]; int bytesToRead = (int)inBuf.Length; int bytesRead = 0; while (bytesToRead > 0) { int n = str.Read(inBuf, bytesRead, bytesToRead); if (n == 0) break; bytesRead += n; bytesToRead -= n; } try { FileStream fstr = new FileStream(strFileSaveFileandPath, FileMode.OpenOrCreate, FileAccess.Write); fstr.Write(inBuf, 0, bytesRead); str.Close(); fstr.Close(); } catch (Exception e) { MessageBox.Show(e.Message); } } private void button2\_Click(object sender, EventArgs e) { if (phpurl.Text == "") { MessageBox.Show("Invalid Url"); } else if (Uri.IsWellFormedUriString(phpurl.Text, UriKind.Absolute) == false) { MessageBox.Show("Invalid Url"); } else { Download(phpurl.Text, @"D:\\test"); } }
Also this is my manifest file
-
Hello everyone This is a program for downloading a file from a url when i download a file i got a error like access to the path is denined can any one tell me why this happen This is my code.
public static void Download(String strURLFileandPath, String strFileSaveFileandPath) { HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(strURLFileandPath); HttpWebResponse ws = (HttpWebResponse)wr.GetResponse(); Stream str = ws.GetResponseStream(); byte\[\] inBuf = new byte\[100000\]; int bytesToRead = (int)inBuf.Length; int bytesRead = 0; while (bytesToRead > 0) { int n = str.Read(inBuf, bytesRead, bytesToRead); if (n == 0) break; bytesRead += n; bytesToRead -= n; } try { FileStream fstr = new FileStream(strFileSaveFileandPath, FileMode.OpenOrCreate, FileAccess.Write); fstr.Write(inBuf, 0, bytesRead); str.Close(); fstr.Close(); } catch (Exception e) { MessageBox.Show(e.Message); } } private void button2\_Click(object sender, EventArgs e) { if (phpurl.Text == "") { MessageBox.Show("Invalid Url"); } else if (Uri.IsWellFormedUriString(phpurl.Text, UriKind.Absolute) == false) { MessageBox.Show("Invalid Url"); } else { Download(phpurl.Text, @"D:\\test"); } }
Also this is my manifest file
-
Hello everyone This is a program for downloading a file from a url when i download a file i got a error like access to the path is denined can any one tell me why this happen This is my code.
public static void Download(String strURLFileandPath, String strFileSaveFileandPath) { HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(strURLFileandPath); HttpWebResponse ws = (HttpWebResponse)wr.GetResponse(); Stream str = ws.GetResponseStream(); byte\[\] inBuf = new byte\[100000\]; int bytesToRead = (int)inBuf.Length; int bytesRead = 0; while (bytesToRead > 0) { int n = str.Read(inBuf, bytesRead, bytesToRead); if (n == 0) break; bytesRead += n; bytesToRead -= n; } try { FileStream fstr = new FileStream(strFileSaveFileandPath, FileMode.OpenOrCreate, FileAccess.Write); fstr.Write(inBuf, 0, bytesRead); str.Close(); fstr.Close(); } catch (Exception e) { MessageBox.Show(e.Message); } } private void button2\_Click(object sender, EventArgs e) { if (phpurl.Text == "") { MessageBox.Show("Invalid Url"); } else if (Uri.IsWellFormedUriString(phpurl.Text, UriKind.Absolute) == false) { MessageBox.Show("Invalid Url"); } else { Download(phpurl.Text, @"D:\\test"); } }
Also this is my manifest file
What line does this exception occur on?? Either the URL you've specified doesn't exist or you don't have permissions to see it or the filepath that you're writing to doesn't exist or you don't have permissions to write to it. I think the meaning of the error is pretty obvious, given your code.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
What line does this exception occur on?? Either the URL you've specified doesn't exist or you don't have permissions to see it or the filepath that you're writing to doesn't exist or you don't have permissions to write to it. I think the meaning of the error is pretty obvious, given your code.
A guide to posting questions on CodeProject[^]
Dave KreskowiakDave Kreskowiak Thanks for your replay this is the line exception occur.
FileStream fstr = new FileStream(strFileSaveFileandPath, FileMode.OpenOrCreate, FileAccess.Write);
Please help me.
Thanks in advance
-
Dave Kreskowiak Thanks for your replay this is the line exception occur.
FileStream fstr = new FileStream(strFileSaveFileandPath, FileMode.OpenOrCreate, FileAccess.Write);
Please help me.
Thanks in advance
-
Also make sure that file doesn't already exist on your system and is not open by another program (or even your own).
The shout of progress is not "Eureka!" it's "Strange... that's not what i expected". - peterchen
Hi dybs, That is a empty folder and there is no way to use the file by any other program. Please help me. Best regards,
Thanks in advance
-
Hi dybs, That is a empty folder and there is no way to use the file by any other program. Please help me. Best regards,
Thanks in advance
The user that is running your code does not have permissions to write to either D:\ or to D:\test. Or, the filepath you specified in that line of code (use the debugger to find out what it is) doesn't exist.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
The user that is running your code does not have permissions to write to either D:\ or to D:\test. Or, the filepath you specified in that line of code (use the debugger to find out what it is) doesn't exist.
A guide to posting questions on CodeProject[^]
Dave KreskowiakHi thanks for the replay I found what is happening here
Download(textBox1.Text, @"D:\test\test.zip");
i have to tell destination file name and its extention!!!. now my problem is when i download larger files (50 mb ) from my server it is not downloading the file. i thing the problem is here.
byte[] inBuf = new byte[100000];
I thing the array is small so how can i manage it? Please help me to do this...
Thanks in advance...
-
Hi thanks for the replay I found what is happening here
Download(textBox1.Text, @"D:\test\test.zip");
i have to tell destination file name and its extention!!!. now my problem is when i download larger files (50 mb ) from my server it is not downloading the file. i thing the problem is here.
byte[] inBuf = new byte[100000];
I thing the array is small so how can i manage it? Please help me to do this...
Thanks in advance...
Any one? Please help me. :confused::confused::confused::confused:
Thanks in advance.
-
Hi thanks for the replay I found what is happening here
Download(textBox1.Text, @"D:\test\test.zip");
i have to tell destination file name and its extention!!!. now my problem is when i download larger files (50 mb ) from my server it is not downloading the file. i thing the problem is here.
byte[] inBuf = new byte[100000];
I thing the array is small so how can i manage it? Please help me to do this...
Thanks in advance...
-
What you should do to avoid your buffer overflowing is to read some data to your buffer and then writing to the file, all within a loop. This is a very common technique, for example, to copy files.
Thanks for your replay. can you explain your answer for better understanding?
Thanks in advance.
-
Any one? Please help me. :confused::confused::confused::confused:
Thanks in advance.
Have some patience. You posted this when the regular who answer questions (on their own time!) were either going to bed or were already there.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
Thanks for your replay. can you explain your answer for better understanding?
Thanks in advance.
You can try something like this. I have modified your code slightly to place reading and writing inside the main loop. Also, I recommend having your streams inside a using block, it is simpler to write and safer if any exception is thrown. Note: This code is untested!!
public static void Download(String strURLFileandPath, String strFileSaveFileandPath) { HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(strURLFileandPath); HttpWebResponse ws = (HttpWebResponse)wr.GetResponse(); byte\[\] inBuf = new byte\[100000\]; int bytesToRead = (int)inBuf.Length; int bytesRead; using (Stream str = ws.GetResponseStream()) { using (FileStream fstr = new FileStream(strFileSaveFileandPath, FileMode.OpenOrCreate, FileAccess.Write)) { try { while ((bytesRead = str.Read(inBuf, 0, bytesToRead)) > 0) { fstr.Write(inBuf, 0, bytesRead); } } catch (Exception e) { MessageBox.Show(e.Message); } } } }