File Permission
-
I was just trying to give file premission to my dummy files but encountered a problem. I found out that only the last applied permission is imposed on the file while rest are overriden or not applied.
using System;
using System.IO;
public class RegistryEditor
{
public static void Main()
{
try
{
FileInfo f = new FileInfo(@"C:\Test.txt");
f.Attributes = FileAttributes.System;
FileInfo f1 = new FileInfo(@"C:\Test.txt");
f1.Attributes = FileAttributes.ReadOnly;
FileInfo f2 = new FileInfo(@"C:\Test.txt");
f2.Attributes = FileAttributes.Hidden;
FileInfo i = new FileInfo(@"C:\Test1.txt");
i.Attributes = FileAttributes.System;
i.Attributes = FileAttributes.Hidden;
i.Attributes = FileAttributes.ReadOnly;
}
catch (Exception ex)
{
Console.Write(ex.ToString());
}
}
}Banking establishments are more dangerous then standing armies.
-
I was just trying to give file premission to my dummy files but encountered a problem. I found out that only the last applied permission is imposed on the file while rest are overriden or not applied.
using System;
using System.IO;
public class RegistryEditor
{
public static void Main()
{
try
{
FileInfo f = new FileInfo(@"C:\Test.txt");
f.Attributes = FileAttributes.System;
FileInfo f1 = new FileInfo(@"C:\Test.txt");
f1.Attributes = FileAttributes.ReadOnly;
FileInfo f2 = new FileInfo(@"C:\Test.txt");
f2.Attributes = FileAttributes.Hidden;
FileInfo i = new FileInfo(@"C:\Test1.txt");
i.Attributes = FileAttributes.System;
i.Attributes = FileAttributes.Hidden;
i.Attributes = FileAttributes.ReadOnly;
}
catch (Exception ex)
{
Console.Write(ex.ToString());
}
}
}Banking establishments are more dangerous then standing armies.
Hi,
int i;
i=1;
i=2;
i=4;Now what is the final value of i? Is it 7? Why would it be any different with file attributes? File attributes are flags, i.e. they have a binary pattern with one bit set, all others cleared. So you can OR them together (with the '|' operator) to get combinations of them. :)
Luc Pattyn
Have a look at my entry for the lean-and-mean competition; please provide comments, feedback, discussion, and don’t forget to vote for it! Thank you.
Local announcement (Antwerp region): Lange Wapper? Neen!
-
I was just trying to give file premission to my dummy files but encountered a problem. I found out that only the last applied permission is imposed on the file while rest are overriden or not applied.
using System;
using System.IO;
public class RegistryEditor
{
public static void Main()
{
try
{
FileInfo f = new FileInfo(@"C:\Test.txt");
f.Attributes = FileAttributes.System;
FileInfo f1 = new FileInfo(@"C:\Test.txt");
f1.Attributes = FileAttributes.ReadOnly;
FileInfo f2 = new FileInfo(@"C:\Test.txt");
f2.Attributes = FileAttributes.Hidden;
FileInfo i = new FileInfo(@"C:\Test1.txt");
i.Attributes = FileAttributes.System;
i.Attributes = FileAttributes.Hidden;
i.Attributes = FileAttributes.ReadOnly;
}
catch (Exception ex)
{
Console.Write(ex.ToString());
}
}
}Banking establishments are more dangerous then standing armies.
You are going to kick yourself! You are, you are...
Ishaan Karnik wrote:
FileInfo f = new FileInfo(@"C:\\Test.txt"); f.Attributes = FileAttributes.System;
Assume it works: the attributes for the file are now "System"
Ishaan Karnik wrote:
FileInfo f1 = new FileInfo(@"C:\\Test.txt"); f1.Attributes = FileAttributes.ReadOnly;
Assume it works: the attributes for the file are now "ReadOnly" Had you considered?
FileInfo f1 = new FileInfo(@"C:\\Test.txt"); f1.Attributes += FileAttributes.ReadOnly;
What are the attributes now? I told you, you would kick yourself!
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones
-
You are going to kick yourself! You are, you are...
Ishaan Karnik wrote:
FileInfo f = new FileInfo(@"C:\\Test.txt"); f.Attributes = FileAttributes.System;
Assume it works: the attributes for the file are now "System"
Ishaan Karnik wrote:
FileInfo f1 = new FileInfo(@"C:\\Test.txt"); f1.Attributes = FileAttributes.ReadOnly;
Assume it works: the attributes for the file are now "ReadOnly" Had you considered?
FileInfo f1 = new FileInfo(@"C:\\Test.txt"); f1.Attributes += FileAttributes.ReadOnly;
What are the attributes now? I told you, you would kick yourself!
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones
-
Except that if it already was ReadOnly, it will be Hidden and not ReadOnly anymore. (ReadOnly = 1, Hidden = 2)
Yes you are right, I'll start kicking myself when I stop typing: it should be "|=" not "+=" :doh:
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones
-
You are going to kick yourself! You are, you are...
Ishaan Karnik wrote:
FileInfo f = new FileInfo(@"C:\\Test.txt"); f.Attributes = FileAttributes.System;
Assume it works: the attributes for the file are now "System"
Ishaan Karnik wrote:
FileInfo f1 = new FileInfo(@"C:\\Test.txt"); f1.Attributes = FileAttributes.ReadOnly;
Assume it works: the attributes for the file are now "ReadOnly" Had you considered?
FileInfo f1 = new FileInfo(@"C:\\Test.txt"); f1.Attributes += FileAttributes.ReadOnly;
What are the attributes now? I told you, you would kick yourself!
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones
OriginalGriff, Yes, i had considered
FileInfo i = new FileInfo(@"C:\Test.txt"); i.Attributes += FileAttributes.ReadOnly;
But this gives an error Operator ' += ' cannot be applied to operands of type 'System.IO.FileAttributes' and 'System.IO.FileAttributes'. Luc Pattyn, ThankYou. Your suggestion worked. -
OriginalGriff, Yes, i had considered
FileInfo i = new FileInfo(@"C:\Test.txt"); i.Attributes += FileAttributes.ReadOnly;
But this gives an error Operator ' += ' cannot be applied to operands of type 'System.IO.FileAttributes' and 'System.IO.FileAttributes'. Luc Pattyn, ThankYou. Your suggestion worked.you're welcome. :)
Luc Pattyn
Have a look at my entry for the lean-and-mean competition; please provide comments, feedback, discussion, and don’t forget to vote for it! Thank you.
Local announcement (Antwerp region): Lange Wapper? Neen!