illustrates the FileAttributes enumeration
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
Example15_4.cs illustrates the FileAttributes enumeration
*/
using System;
using System.Windows.Forms;
using System.IO;
public class Example15_4
{
// the DecipherAttributes method turns file attributes
// into something easier for people to read
public static void DecipherAttributes(FileAttributes f)
{
if ((f & FileAttributes.Archive) == FileAttributes.Archive)
Console.WriteLine("Archive");
if ((f & FileAttributes.Compressed) == FileAttributes.Compressed)
Console.WriteLine("Compressed");
if ((f & FileAttributes.Device) == FileAttributes.Device)
Console.WriteLine("Device");
if ((f & FileAttributes.Directory) == FileAttributes.Directory)
Console.WriteLine("Directory");
if ((f & FileAttributes.Encrypted) == FileAttributes.Encrypted)
Console.WriteLine("Encrypted");
if ((f & FileAttributes.Hidden) == FileAttributes.Hidden)
Console.WriteLine("Hidden");
if ((f & FileAttributes.NotContentIndexed) == FileAttributes.NotContentIndexed)
Console.WriteLine("NotContentIndexed");
if ((f & FileAttributes.Offline) == FileAttributes.Offline)
Console.WriteLine("Offline");
if ((f & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
Console.WriteLine("ReadOnly");
if ((f & FileAttributes.ReparsePoint) == FileAttributes.ReparsePoint)
Console.WriteLine("ReparsePoint");
if ((f & FileAttributes.SparseFile) == FileAttributes.SparseFile)
Console.WriteLine("SparseFile");
if ((f & FileAttributes.System) == FileAttributes.System)
Console.WriteLine("System");
if ((f & FileAttributes.Temporary) == FileAttributes.Temporary)
Console.WriteLine("Temporary");
}
[STAThread]
public static void Main()
{
// create and show an open file dialog
OpenFileDialog dlgOpen = new OpenFileDialog();
if (dlgOpen.ShowDialog() == DialogResult.OK)
{
// retrieve and show the file attributes
FileAttributes f = File.GetAttributes(dlgOpen.FileName);
Console.WriteLine("Filename " + dlgOpen.FileName +
" has attributes:");
DecipherAttributes(f);
}
}
}
Related examples in the same category
1. | Get file Creation Time | | |
2. | Delete a file | | |
3. | Get File Access Control | | |
4. | illustrates the FileInfo class | | |
5. | illustrates the FileSystemWatcher class | | |
6. | File class to check whether a file exists, open and read | | |
7. | Uses methods in the File class to check whether a file exists | | |
8. | Uses methods in the File class to check the status of a file | | |
9. | File Class | | |
10. | File.AppendAllLines Method Appends lines to a file, and then closes the file. | | |
11. | File.AppendAllText Opens a file, appends the specified string to the file | | |
12. | File.AppendAllText Method Appends the string to the file, creating the file if it does not exist. | | |
13. | File.AppendText Creates a StreamWriter that appends UTF-8 encoded text to an existing file. | | |
14. | File.Copy Method Copies file to a new file | | |
15. | File.Create Creates or overwrites a file in the specified path. | | |
16. | File.Create Method (String, Int32) Creates or overwrites the specified file. | | |
17. | File.CreateText Method Creates or opens a file for writing UTF-8 encoded text. | | |
18. | File.Decrypt Decrypts a file | | |
19. | File.Exists Determines whether the specified file exists. | | |
20. | File.GetAccessControl Gets a FileSecurity object that encapsulates the access control list | | |
21. | File.GetAttributes Method Gets the FileAttributes of the file on the path. | | |
22. | File.GetCreationTime Method Returns the creation date and time of the specified file or directory. | | |
23. | File.GetLastAccessTime Method Returns the date and time the specified file or directory was last accessed. | | |
24. | File.GetLastWriteTime Returns the date and time the specified file or directory was last written to. | | |
25. | File.Move Moves a file to a new location | | |
26. | File.Open Opens a FileStream on the specified path with read/write access. | | |
27. | File.Open Method (String, FileMode, FileAccess) | | |
28. | File.Open Method (String, FileMode, FileAccess, FileShare) | | |
29. | File.OpenRead Method Opens an existing file for reading. | | |
30. | File.OpenText Method Opens an existing UTF-8 encoded text file for reading. | | |
31. | File.OpenWrite Method Opens an existing file or creates a new file for writing. | | |
32. | File.ReadAllLines Method Opens a text file, reads all lines of the file, and then closes the file. | | |
33. | File.ReadAllLines Method (String, Encoding) | | |
34. | File.ReadLines Method | | |
35. | File.Replace Replaces contents of file | | |
36. | File.SetLastAccessTime Sets the date and time the specified file was last accessed. | | |
37. | File.SetLastWriteTime | | |