简体   繁体   English

无法将文件从网络下载到计算机上的文件夹中

[英]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. 我正在编写一个程序,该程序基本上使用http://dev.maxmind.com/geoip/legacy/geolite/中的国家/地区的DAT文件来对IP地址进行一些分析。

However if the folder in C:\\ProgramData\\IPChecker is not there (first time) I create it - no problem, this works. 但是,如果C:\\ ProgramData \\ IPChecker中的文件夹不存在(第一次),我创建它-没问题,这可行。

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. 但是,如果文件不在文件夹中-这不是我第一次运行该程序,也不是按计划的时间间隔获取DAT文件的最新副本,那么我希望能够从网上下载该文件并放入将其放入此文件夹。

I cannot post any code as the editor won't let me but its a simple DownloadFile command with URL and Path as parameters. 我无法发布任何代码,因为编辑器不允许我这样做,而是使用URL和Path作为参数的简单DownloadFile命令。

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 其中this.DataFolder是我刚刚创建的文件夹的路径,或者要将压缩文件复制到例如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. 我已经手动制作了该文件夹(我具有管理员权限),将路径更改为c:\\的根目录,下载文件夹以及PC上的其他文件夹,但是无论我尝试下载并复制到哪个文件夹中,我总是能得到相同的权限被拒绝错误。

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." 只是“消息=拒绝访问路径'C:\\ ProgramData \\ ProxyHunter'”。 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)" 来自“ System.IO .__ Error.WinIOError(Int32 errorCode,字符串mayFullPath)在System.IO.FileStream.Init(字符串路径,FileMode模式,FileAccess访问,Int32权限,布尔useRights,FileShare共享,Int32 bufferSize, FileOptions选项,SECURITY_ATTRIBUTES secAttrs,字符串msgPath,布尔bFromProxy,布尔useLongPath,布尔checkHost在System.IO.FileStream..ctor(字符串路径,FileMode模式,FileAccess访问)在System.Net.WebClient.DownloadFile(Uri地址,字符串)文档名称)”

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? 任何人都可以认为正在发生什么事情或我需要做什么才能使WebClient.DownloadFile行工作而不会引发权限被拒绝的错误?

I am using .NET 4.5 我正在使用.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. 同样,尽管例外情况表明我今天打开项目时是在VS 2010中(我确实在其中创建了原始项目),但该项目却在VS 2012中打开。所以我不知道这是否与它有关。

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. 基本上,我很讨厌,因为我从中复制的代码提供了文件夹路径作为WebClient.DownloadFile(url,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 有关设置.Net安全策略的更多信息,请参见此处。

Cheers 干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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