1 string path = @"c:\temp\MyTest.txt";
2 // Delete the file if it exists.
3 if(!File.Exists(path))
4 {
5 File.Create(path);
6 }
7 if((File.GetAttributes(path)&FileAttributes.Hidden)==FileAttributes.Hidden)
8 {
9 //Show the file.
10 File.SetAttributes(path,FileAttributes.Archive);
11 Console.WriteLine("The {0} file is no longer hidden.", path);
12 }
13 else
14 {
15 //Hide the file.
16 File.SetAttributes(path, File.GetAttributes(path)|FileAttributes.Hidden);
17 Console.WriteLine("The {0} file is now hidden.", path);
18 }
C # Remove the read-only attributes of files and folders
When we use DirectoryInfo dir = Directory. when CreateDirectory (pathName) is used to create a directory or a File, it must be deleted after it is used up as a temporary File. delete () or Directory. delete () often encounters an "Access Denied error". In this case, we need to set the read-only attribute of the file or folder before deleting it.
Remove the read-only attribute of the folder: System. IO. DirectoryInfo DirInfo = new DirectoryInfo ("filepath ");
DirInfo. Attributes = FileAttributes. Normal & FileAttributes. Directory;
Remove the read-only attribute of the File: System. IO. File. SetAttributes ("filepath", System. IO. FileAttributes. Normal );
FileAttributes Enumeration
Provides file and directory attributes.
This enumeration has a FlagsAttribute attribute that allows its member values to be combined by bit.
Namespace: System. IO
Assembly: mscorlib (in mscorlib. dll)
Syntax
[SerializableAttribute]
[FlagsAttribute]
[ComVisibleAttribute (true)]
Public enum FileAttributes
| |
Member name |
Description |
|
Archive |
The Archiving status of the file. The application uses this property to add a backup or remove tag to the file. |
|
Compressed |
The file is compressed. |
|
Device |
Reserved for future use. |
|
Directory |
File is a directory. |
|
Encrypted |
The file or directory is encrypted. For a file, all data in the file is encrypted. The directory indicates that the newly created files and directories are encrypted by default. |
|
Hidden |
Files are hidden, so they are not included in the normal directory list. |
|
Normal |
The file is normal and no other attributes are set. This attribute is only valid when used separately. |
|
NotContentIndexed |
The Content Indexing Service of the operating system does not create the index of this file. |
|
Offline |
The file is offline. File data cannot be used immediately. |
|
ReadOnly |
The file is read-only. |
|
ReparsePoint |
A file contains a re-analysis point, which is a user-defined data block associated with a file or directory. |
|
SparseFile |
The file is a sparse file. Sparse files are generally large files with zero data. |
|
System |
The file is a system file. Files are part of the operating system or are exclusively used by the operating system. |
|
Temporary |
Files are temporary files. The file system tries to keep all the data in the memory for faster access, instead of refreshing the data back to the large-capacity storage. When a temporary file is no longer needed, the application immediately deletes it. |
Simple Application of FileAttributes
Commonly used FileAttributes members include den, System, Archive, ReadOnly, and Directory. These objects can perform bitfield operations and attach properties to files through bitwise OR operations. For example, if File. setAttribute (filename, FileAttribute. Hidden | FileAttribute. ReadOnly) is used, the filename File has the Hidden and ReadOnly attributes.
Perform the bitwise "and" operation between the value and the flag enumerated constant to test whether the flag has been set in the value, in this method, all bits in the value that do not correspond to the flag are set to zero, and then the result of this operation is equal to the enumerated constant of the flag.
It is still an example in MSDN:
Using System;
Using System. IO;
Using System. Text;
Class Test
{
Public static void Main ()
{
String path = @ "c: \ temp \ MyTest.txt ";
// Create the file if it does not exist.
If (! File. Exists (path ))
{
File. Create (path );
}
If (File. GetAttributes (path) & FileAttributes. Hidden) = FileAttributes. Hidden)
{
// Show the file.
File. SetAttributes (path, FileAttributes. Archive );
Console. WriteLine ("The {0} file is no longer hidden.", path );
}
Else
{
// Hide the file.
File. SetAttributes (path, File. GetAttributes (path) | FileAttributes. Hidden );
Console. WriteLine ("The {0} file is now hidden.", path );
}
}
}
Ps: FileAttributes. Archive is the archiving status of the document. The application uses this attribute to add a backup or deletion mark to the file.
When we use DirectoryInfo dir = Directory. when CreateDirectory (pathName) is used to create a directory or a File, it must be deleted after it is used up as a temporary File. delete () or Directory. delete () often encounters an "Access Denied error". In this case, we need to set the read-only attribute of the file or folder before deleting it.
Remove the read-only attribute of the folder: System. IO. DirectoryInfo DirInfo = new DirectoryInfo ("filepath ");
DirInfo. Attributes = FileAttributes. Normal & FileAttributes. Directory;
Remove the read-only attribute of the File: System. IO. File. SetAttributes ("filepath", System. IO. FileAttributes. Normal );
FileAttributes Enumeration
Provides file and directory attributes.
This enumeration has a FlagsAttribute attribute that allows its member values to be combined by bit.
Namespace: System. IO
Assembly: mscorlib (in mscorlib. dll)
Syntax
[SerializableAttribute]
[FlagsAttribute]
[ComVisibleAttribute (true)]
Public enum FileAttributes
| |
Member name |
Description |
|
Archive |
The Archiving status of the file. The application uses this property to add a backup or remove tag to the file. |
|
Compressed |
The file is compressed. |
|
Device |
Reserved for future use. |
|
Directory |
File is a directory. |
|
Encrypted |
The file or directory is encrypted. For a file, all data in the file is encrypted. The directory indicates that the newly created files and directories are encrypted by default. |
|
Hidden |
Files are hidden, so they are not included in the normal directory list. |
|
Normal |
The file is normal and no other attributes are set. This attribute is only valid when used separately. |
|
NotContentIndexed |
The Content Indexing Service of the operating system does not create the index of this file. |
|
Offline |
The file is offline. File data cannot be used immediately. |
|
ReadOnly |
The file is read-only. |
|
ReparsePoint |
A file contains a re-analysis point, which is a user-defined data block associated with a file or directory. |
|
SparseFile |
The file is a sparse file. Sparse files are generally large files with zero data. |
|
System |
The file is a system file. Files are part of the operating system or are exclusively used by the operating system. |
|
Temporary |
Files are temporary files. The file system tries to keep all the data in the memory for faster access, instead of refreshing the data back to the large-capacity storage. When a temporary file is no longer needed, the application immediately deletes it. |
Simple Application of FileAttributes
Commonly used FileAttributes members include den, System, Archive, ReadOnly, and Directory. These objects can perform bitfield operations and attach properties to files through bitwise OR operations. For example, if File. setAttribute (filename, FileAttribute. Hidden | FileAttribute. ReadOnly) is used, the filename File has the Hidden and ReadOnly attributes.
Perform the bitwise "and" operation between the value and the flag enumerated constant to test whether the flag has been set in the value, in this method, all bits in the value that do not correspond to the flag are set to zero, and then the result of this operation is equal to the enumerated constant of the flag.
It is still an example in MSDN:
Using System;
Using System. IO;
Using System. Text;
Class Test
{
Public static void Main ()
{
String path = @ "c: \ temp \ MyTest.txt ";
// Create the file if it does not exist.
If (! File. Exists (path ))
{
File. Create (path );
}
If (File. GetAttributes (path) & FileAttributes. Hidden) = FileAttributes. Hidden)
{
// Show the file.
File. SetAttributes (path, FileAttributes. Archive );
Console. WriteLine ("The {0} file is no longer hidden.", path );
}
Else
{
// Hide the file.
File. SetAttributes (path, File. GetAttributes (path) | FileAttributes. Hidden );
Console. WriteLine ("The {0} file is now hidden.", path );
}
}
}
Ps: FileAttributes. Archive is the archiving status of the document. The application uses this attribute to add a backup or deletion mark to the file.