0

Im saving images from a URL list but trying to duplicate the folder structure locally.

I parse the URL to give me the folder structure I want :

Example:

URL =  www.site.com/images/folder1/folder2/image

My local base folder is mydocs/site/images I split the url string up and am able to recursively create the proper folder structure using:

 if (!Directory.Exists(finalLocalFolder))
    {
        DirectoryInfo di = Directory.CreateDirectory(finalLocalFolder);
    }

Everything works great UNTIL I try and save the image to the folder using :

    WebClient webClient = new WebClient();
    webClient.DownloadFile(remoteUrl, finalLocalFolder);

In which case, I am told that access to that folder is denied.

"System.UnauthorizedAccessException: Access to the path 'mydocs\images\test\test1\test\2\3m' is denied."

So I am guessing that I need to create a step in the CREATEDIRECTORY area where I immediately grant access to that folder.

Is there an easy way to do this?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Todd Vance
  • 4,627
  • 7
  • 46
  • 66
  • That's odd. Folders generally inherit the permissions of their parent which also usually includes the Creator/Owner having full permission. Are you trying to save to "Program Files" or something? – lc. Jan 23 '13 at 19:15
  • I don't mean the type of file you are trying to save, I mean the `%ProgramFiles%` folder – lc. Jan 23 '13 at 19:17
  • `'mydocs\images\test\test1\test\2\3m'` doesn't look like a complete path to me. Seems something like `C:\` is missing at the beginning? – Uwe Keim Jan 23 '13 at 19:18
  • 1
    NO! BUT!!! you got me looking and thinking. This is my bad...so sorry. Ill delete this -- I forgot to give the final path an actual FILE name (DOH!) – Todd Vance Jan 23 '13 at 19:18
  • 1
    @UweKeim -- that was just for an example Uwe. – Todd Vance Jan 23 '13 at 19:19

1 Answers1

1

I can close this if you would like - but just to put up what my problem was... here it is:

In here: webClient.DownloadFile(remoteUrl, finalLocalFolder);

finalLocalFolder was ending up something like: "C:\mydocs\images\test\test2" when IT SHOULD HAVE BEEN "C:\mydocs\images\test\test2\theimagefilename.jpg"

Stupid moment. Sorry.

Todd Vance
  • 4,627
  • 7
  • 46
  • 66