C Board  

Go Back   C Board > General Programming Boards > C# Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 09-12-2009, 10:45 AM   #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
jamietrent is offline   Reply With Quote
Old 09-12-2009, 10:53 AM   #2
Registered User
 
C_ntua's Avatar
 
Join Date: Jun 2008
Posts: 1,134
What is the error message?
C_ntua is offline   Reply With Quote
Old 09-12-2009, 11:11 AM   #3
Registered User
 
Join Date: Sep 2009
Posts: 19
umm hold on a sec
jamietrent is offline   Reply With Quote
Old 09-12-2009, 11:12 AM   #4
Registered User
 
Join Date: Sep 2009
Posts: 19
invalid URI
jamietrent is offline   Reply With Quote
Old 09-12-2009, 11:23 AM   #5
Registered User
 
Join Date: Mar 2009
Location: england
Posts: 76
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();
        }
theoobe is offline   Reply With Quote
Old 09-12-2009, 11:27 AM   #6
Registered User
 
Join Date: Sep 2009
Posts: 19
it still does it
jamietrent is offline   Reply With Quote
Old 09-12-2009, 11:30 AM   #7
Registered User
 
Join Date: Mar 2009
Location: england
Posts: 76
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.
theoobe is offline   Reply With Quote
Old 09-12-2009, 11:32 AM   #8
Registered User
 
Join Date: Sep 2009
Posts: 19
its for my server here is the adress
68.214.84.27/string.wz
jamietrent is offline   Reply With Quote
Old 09-12-2009, 11:34 AM   #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");
jamietrent is offline   Reply With Quote
Old 09-12-2009, 11:37 AM   #10
Registered User
 
Join Date: Mar 2009
Location: england
Posts: 76
The code I posted earlier, plus...

Code:
DownloadFileFromURL("http://68.214.84.27/string.wz", "string.wz");
...seems to work for me. :-/
theoobe is offline   Reply With Quote
Old 09-12-2009, 11:38 AM   #11
Registered User
 
Join Date: Sep 2009
Posts: 19
ummm let me try again
jamietrent is offline   Reply With Quote
Old 09-12-2009, 11:43 AM   #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
jamietrent is offline   Reply With Quote
Old 09-12-2009, 11:49 AM   #13
Registered User
 
Join Date: Mar 2009
Location: england
Posts: 76
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.
theoobe is offline   Reply With Quote
Old 09-12-2009, 12:04 PM   #14
Registered User
 
Join Date: Sep 2009
Posts: 19
please tell me how to get it to work
jamietrent is offline   Reply With Quote
Old 09-12-2009, 12:51 PM   #15
Registered User
 
C_ntua's Avatar
 
Join Date: Jun 2008
Posts: 1,134
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
C_ntua is offline   Reply With Quote
Reply

Tags
[code]

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Consuming same Web Service multiple times cfriend C# Programming 2 01-10-2006 09:59 AM
writing text over a deleted button algi Windows Programming 4 05-02-2005 11:32 AM
BN_CLICKED, change button style bennyandthejets Windows Programming 9 09-11-2002 03:59 AM


All times are GMT -6. The time now is 09:08 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22