views:

56

answers:

2

Hi all, I'm using a FileStream to download information of an FTP server to a directory on my C:\ drive. For some reason, even though I've even tried setting the directory permissions to even 'Everyone' access, it's given me this exception:

System.UnauthorizedAccessException: Access to the path 'C:\tmpfolder' is denied'

Why is this? Here is an extract of my code.

byte[] fileData = request.DownloadData(dataMap.GetString("ftpPath") + "/" + content);
file = new FileStream(@"C:\tmpfolder", FileMode.Create, FileAccess.Write);
downloadedlocation = file.ToString();
file.Write(fileData, 0, fileData.Length);

Also, my program is not in ASP.NET and is just a C# console app.

A: 

I would guess that you do not have write privilege to c:\ where you try to create a file named tmpfolder.

If a folder named tmpfolder exists in your c:\ change your code to

file = new FileStream(@"C:\tmpfolder\myfile.tmp", FileMode.Create, FileAccess.Write);

hth

Mario

EDIT: On a further note: check out this link http://stackoverflow.com/questions/20146/how-to-create-a-temporary-file-for-writing-to-in-c , you may need it if you have multiple file operations going on at the same time. Do no forget to delete the files afterwards.

Mario The Spoon
+3  A: 

If it doesn't matter where to store the file, try

using System.IO;
.
.
.
string tempFile = Path.GetTempFileName();

This will create a temporary file in your account temp folder. No concerns about permissions ;-)

Dennis