user194076
user194076

Reputation: 9027

Access to a newly created folder is denied

I'm writing a file on a network drive into a new folder.

string directory=@"\\dir\test\"
string folderName = "testFolder " + DateTimeOffset.Now.UtcDateTime.ToString().Replace("/","-").Replace(":","-");

string newPath = Path.Combine(directory, folderName);

        if (!Directory.Exists(newPath))
        {
            Directory.CreateDirectory(newPath);
        }
        if (!File.Exists(newPath))
        {
            using (System.IO.FileStream fs = File.Create(newPath))
            {
                fs.Write(data, 0, data.Length);
            }
        }

on line: using (System.IO.FileStream fs = File.Create(newPath)) - it errors out saying:

Access to the path '\dir\test\testFolder 3-19-2012 11-58-43 PM' is denied.

I've to create a directory with DirectorySecurity allowing read and write. still receiving an error.

I've been following the following topic to create folder and file: http://msdn.microsoft.com/en-us/library/as2f1fez.aspx

I'm able to write to the directory @"\\dir\test\" with no problems.

Upvotes: 0

Views: 517

Answers (1)

Balazs Tihanyi
Balazs Tihanyi

Reputation: 6719

You are using the same newPath for the directory and file.

Upvotes: 2

Related Questions