Click here to Skip to main content
15,898,939 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
string folderName = c:\Activity Logging;
string fileName = Log File.txt;
string path = Path.Combine(folderName, fileName);

if (!Directory.Exists(folderName))
{
    Directory.CreateDirectory(folderName); //Working
    fileCheck = true;
}

if (!File.Exists(path))
{
      File.Create(path, 1024, FileOptions.RandomAccess, new FileSecurity(path, System.Security.AccessControl.AccessControlSections.All));// Error Here??

    fileCheck = true;
 }
Posted
Updated 11-Aug-14 14:37pm
v4
Comments
Adam Zgagacz 11-Aug-14 16:58pm    
looks like error is coming from:
new FileSecurity(path, System.Security.AccessControl.AccessControlSections.All);

when you are executing it file does not exist yet.
Sergey Alexandrovich Kryukov 11-Aug-14 17:08pm    
You are right. I can confirm: this is the only problem. If this argument is omitted, it will work. You can post it as your answer.
—SA
Sergey Alexandrovich Kryukov 11-Aug-14 17:04pm    
Why doing it this way? What's wrong with creating reader or writer? (Asking just in case.)
—SA

Error is coming from:

new FileSecurity(path, System.Security.AccessControl.AccessControlSections.All);

You are trying to refer file that was not yet created.

I have same questions/doubts as Sergey: what are you trying to achieve? Is FileSecurity argument essential for you? Can you omit it?

(Just curious)
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 12-Aug-14 0:28am    
Correct, a 5.
—SA
Admire Mhlaba 12-Aug-14 3:41am    
Thank you all for your kind and quick responses. FileSecurity argument is essential because without it I get another exception raised by lack of administrative privileges to read/write to files in C:// drive.
if (!File.Exists(path))
{
File.Create(path, 1024, FileOptions.RandomAccess, new FileSecurity());

FileSecurity fSecurity = File.GetAccessControl(path);

fSecurity.AddAccessRule(new FileSystemAccessRule("Everyone",FileSystemRights.FullControl, AccessControlType.Allow));

File.SetAccessControl(path, fSecurity);

fileCheck = true;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900