简体   繁体   中英

Trouble downloading a file from the web to a folder on my computer

I am writing a program that basically uses a DAT file of countries from http://dev.maxmind.com/geoip/legacy/geolite/ to run some analysis of IP addresses.

However if the folder in C:\\ProgramData\\IPChecker is not there (first time) I create it - no problem, this works.

However if the file is not in the folder - which it won't be the first time I run the program or at scheduled intervals to get latest copies of the DAT file, I want to be able to download the file from the web and put it into this folder.

I cannot post any code as the editor won't let me but its a simple DownloadFile command with URL and Path as parameters.

Where this.DataFolder is the path to the folder I have either just created OR want to copy the zipped file into eg C:\\ProgramData\\IPChecker

I am then going to unzip the file so it can be used - if I get that far.

I thought these two lines of code would do what I wanted.

However I keep getting an error saying I don't have sufficient privileges to move the file into the folder.

I have manually made the folder (I have admin rights), changed the path to the root of c:\\, the downloads folder and various other folders on my PC but whatever folder I am trying to download and copy into I always seem to get the same permission denied error.

I have tried pasting the whole exception in this box but again the editor won't let me save the post if I do. It's just a "Message=Access to the path 'C:\\ProgramData\\ProxyHunter' is denied." error with StackTrace from "at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access) at System.Net.WebClient.DownloadFile(Uri address, String fileName)"

Can anyone think what is going on or what I need to do to get the WebClient.DownloadFile line to work without throwing a permission denied error?

I am using .NET 4.5

Also although the exception says I am in VS 2010 (where I did create the original project) when I opened the project today it opened in VS 2012. So I don't know if that has anything to do with it or not.

Any help would be much appreciated. Thanks

Right I've fixed it. Basically I was being a nob as the code I was copying from was providing a folder path as the 2nd parameter to WebClient.DownloadFile(url,path) and not a file path.

Once I changed the path to a file path and not just a folder the file was downloaded to that location correctly.

Thanks for all your help!

Are you attempting to run the program from a network share? If so the program could be restricted from writing to the local drive due to your machine security policy. Try copying the program to your local disk and running from there.

If it works locally, then I suggest taking a look at other questions like this one

Further information on setting .Net security policies can be found here

Cheers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM