Chris
Chris

Reputation: 7621

FileStream not letting me create file on local machine?

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.

Upvotes: 2

Views: 4270

Answers (2)

Dennis
Dennis

Reputation: 2142

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 ;-)

Upvotes: 4

Mario The Spoon
Mario The Spoon

Reputation: 4869

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 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.

Upvotes: 0

Related Questions