Thread: (HELP) i need a button that downloads something from a web adress

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    19

    Arrow (HELP) i need a button that downloads something from a web adress

    ok i have this code

    here it is

    Code:
    void DownloadFileFromURL(string URL, string SaveFileName)
            {
                Uri u = new Uri(URL);
                Stream s = ((HttpWebRequest)HttpWebRequest.Create(u)).GetResponse().GetResponseStream();
                FileStream f = File.Create(SaveFileName);
                byte[] r = new byte[4096];
                while (true)
                {
                    int num = s.Read(r, 0, 4096);
                    if (num < 1)
                        break;
                    f.Write(r, 0, num);
                }
            }
    when i debug and i click the button it takes me to the code and it highlights this part of the code

    Uri u = new Uri(URL);

    please help me get this to work

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    What is the error message?

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    19
    umm hold on a sec

  4. #4
    Registered User
    Join Date
    Sep 2009
    Posts
    19
    invalid URI

  5. #5
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    Code:
            void DownloadFileFromURL(String URL, String SaveFileName)
            {
                WebRequest request = WebRequest.Create(URL.StartsWith("http://") ? URL : ("http://" + URL));
                WebResponse response = request.GetResponse();
                Stream stream = response.GetResponseStream();
                FileStream file = new FileStream(SaveFileName, FileMode.Create, FileAccess.Write);
    
                while (true)
                {
                    byte[] buffer = new byte[4096];
                    int size = stream.Read(buffer, 0, 4096);
    
                    if (size > 0)
                        file.Write(buffer, 0, size);
                    else
                        break;
                }
    
                file.Close();
                stream.Close();
                response.Close();
            }

  6. #6
    Registered User
    Join Date
    Sep 2009
    Posts
    19
    it still does it

  7. #7
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    What URL are you using? It worked when I tested it with www.google.com
    Last edited by theoobe; 09-12-2009 at 11:32 AM.

  8. #8
    Registered User
    Join Date
    Sep 2009
    Posts
    19
    its for my server here is the adress
    68.214.84.27/string.wz

  9. #9
    Registered User
    Join Date
    Sep 2009
    Posts
    19
    here is the code in the button
    that downloads it

    i need it to work with this

    progressBar1.Value += 10
    DownloadFileFromURL("68.214.84.27/string.wz", "C:\\program files\\globalc\\cryptonyte");

  10. #10
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    The code I posted earlier, plus...

    Code:
    DownloadFileFromURL("http://68.214.84.27/string.wz", "string.wz");
    ...seems to work for me. :-/

  11. #11
    Registered User
    Join Date
    Sep 2009
    Posts
    19
    ummm let me try again

  12. #12
    Registered User
    Join Date
    Sep 2009
    Posts
    19
    it comes up with an error that says access to the path has been denided

    well the path is true but it still says that

  13. #13
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    You can only write/modify files in the program files folder, if you have UAC (User Account Control) disabled. I dare say that is why you are denied access.

    One area of your hard disk which will still have write/modify access is the Application Data folder. You can locate the path to this folder using

    Code:
    Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
    It is good practice to use this area to store your application's data files.

  14. #14
    Registered User
    Join Date
    Sep 2009
    Posts
    19
    please tell me how to get it to work

  15. #15
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    theoobe is saying that you cannot save the file at
    Code:
    C:\\program files\\globalc\\cryptonyte
    . So you need to choose a different location

    The
    Code:
    Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
    returns a string with the Application Data folder, in which you can write.

    So write in your Application Data folder and not in Program Files.

    Don't know how you can change permissions and be able to write in Program Files

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BN_CLICKED, change button style
    By bennyandthejets in forum Windows Programming
    Replies: 13
    Last Post: 07-05-2010, 11:42 PM
  2. Consuming same Web Service multiple times
    By cfriend in forum C# Programming
    Replies: 2
    Last Post: 01-10-2006, 09:59 AM
  3. writing text over a deleted button
    By algi in forum Windows Programming
    Replies: 4
    Last Post: 05-02-2005, 11:32 AM

Tags for this Thread