Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Access to the path is denied

Access to the path is denied

Scheduled Pinned Locked Moved C#
help
13 Posts 5 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Arunkumar Koloth

    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

    D Offline
    D Offline
    Dave Kreskowiak
    wrote on last edited by
    #3

    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

    A 1 Reply Last reply
    0
    • D 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 Kreskowiak

      A Offline
      A Offline
      Arunkumar Koloth
      wrote on last edited by
      #4

      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

      D 1 Reply Last reply
      0
      • A Arunkumar Koloth

        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

        D Offline
        D Offline
        dybs
        wrote on last edited by
        #5

        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

        A 1 Reply Last reply
        0
        • D dybs

          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

          A Offline
          A Offline
          Arunkumar Koloth
          wrote on last edited by
          #6

          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

          D 1 Reply Last reply
          0
          • A Arunkumar Koloth

            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

            D Offline
            D Offline
            Dave Kreskowiak
            wrote on last edited by
            #7

            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

            A 1 Reply Last reply
            0
            • D 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 Kreskowiak

              A Offline
              A Offline
              Arunkumar Koloth
              wrote on last edited by
              #8

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

              A I 2 Replies Last reply
              0
              • A Arunkumar Koloth

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

                A Offline
                A Offline
                Arunkumar Koloth
                wrote on last edited by
                #9

                Any one? Please help me. :confused::confused::confused::confused:

                Thanks in advance.

                D 1 Reply Last reply
                0
                • A Arunkumar Koloth

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

                  I Offline
                  I Offline
                  ignrod
                  wrote on last edited by
                  #10

                  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.

                  A 1 Reply Last reply
                  0
                  • I ignrod

                    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.

                    A Offline
                    A Offline
                    Arunkumar Koloth
                    wrote on last edited by
                    #11

                    Thanks for your replay. can you explain your answer for better understanding?

                    Thanks in advance.

                    I 1 Reply Last reply
                    0
                    • A Arunkumar Koloth

                      Any one? Please help me. :confused::confused::confused::confused:

                      Thanks in advance.

                      D Offline
                      D Offline
                      Dave Kreskowiak
                      wrote on last edited by
                      #12

                      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

                      1 Reply Last reply
                      0
                      • A Arunkumar Koloth

                        Thanks for your replay. can you explain your answer for better understanding?

                        Thanks in advance.

                        I Offline
                        I Offline
                        ignrod
                        wrote on last edited by
                        #13

                        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);
                                        }
                                    }
                                }
                            }
                        
                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups