C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Note: The Attributes property returns an enumerated constant that is encoded as enum flags. You can individually test these flags.
Enum FlagsC# program that uses Attributes
using System;
using System.IO;
class Program
{
static void Main()
{
// Get Attributes for file.
FileInfo info = new FileInfo("C:\\file.txt");
FileAttributes attributes = info.Attributes;
Console.WriteLine(attributes);
// Get Attributes for directory.
info = new FileInfo("C:\\");
attributes = info.Attributes;
Console.WriteLine(attributes);
}
}
Output
Archive
Hidden, System, Directory
FileAttributes values:
FileAttributes.Archive
FileAttributes.Compressed
FileAttributes.Device
FileAttributes.Directory
FileAttributes.Encrypted
FileAttributes.Hidden
FileAttributes.Normal
FileAttributes.NotContentIndexed
FileAttributes.Offline
FileAttributes.ReadOnly
FileAttributes.ReparsePoint
FileAttributes.SparseFile
FileAttributes.System
FileAttributes.Temporary
Here: We access the CreationTime, LastAccessTime and LastWriteTime properties. The program prints these values.
ConsoleUtc time: You can also get the creation, last access, and last write times in Coordinated Universal Time.
C# program that uses Time properties
using System;
using System.IO;
class Program
{
static void Main()
{
FileInfo info = new FileInfo("C:\\file.txt");
DateTime time = info.CreationTime;
Console.WriteLine(time);
time = info.LastAccessTime;
Console.WriteLine(time);
time = info.LastWriteTime;
Console.WriteLine(time);
}
}
Output
7/17/2010 9:48:48 AM
7/17/2010 9:48:48 AM
8/18/2010 4:48:27 PM
Info: Alternatively, you can get just the name of the directory with the DirectoryName string property.
DirectoryC# program that uses Directory property
using System;
using System.IO;
class Program
{
static void Main()
{
// Get file info.
FileInfo info = new FileInfo("C:\\file.txt");
// Access parent directory.
DirectoryInfo dir = info.Directory;
Console.WriteLine(dir.Name);
// Get directory name.
string name = info.DirectoryName;
Console.WriteLine(name);
}
}
Output
C:\
C:\
C# program that uses Exists property
using System;
using System.IO;
class Program
{
static void Main()
{
// Get file info.
FileInfo info = new FileInfo("C:\\does-not-exist.txt");
bool exists = info.Exists;
Console.WriteLine(exists);
}
}
Output
False
C# program that uses file name properties
using System;
using System.IO;
class Program
{
static void Main()
{
// Get file info.
FileInfo info = new FileInfo("C:\\file.txt");
string name = info.Name;
string fullName = info.FullName;
string extension = info.Extension; // Has period
Console.WriteLine(name);
Console.WriteLine(fullName);
Console.WriteLine(extension);
}
}
Output
file.txt
C:\file.txt
.txt
C# program that uses Length property
using System;
using System.IO;
class Program
{
static void Main()
{
FileInfo info = new FileInfo("C:\\a");
long value = info.Length;
Console.WriteLine(value);
}
}
Output
5320683
Also: CopyTo and Replace provide parallel functionality to the MoveTo method. Instead of renaming, CopyTo makes a copy of the file.
And: The Replace method allows you to copy a file to an existing location and also make a backup file.
C# program that uses MoveTo method on FileInfo
using System.IO;
class Program
{
static void Main()
{
// Write the specified file with some text.
File.WriteAllText("C:\\test1.txt", "A");
// Create a FileInfo instance for the specified path.
// ... Then move the specified file to a new file path.
FileInfo info = new FileInfo("C:\\test1.txt");
info.MoveTo("C:\\test2.txt");
}
}
Output
1. One file is created.
2. The file is renamed to a new name.
Using: You should wrap these in the using-resource-acquisition statement. This improves resource usage.
UsingTip: Directly creating a StreamWriter or Reader is a simpler pattern, one more common, and it should be preferred.
StreamWriterStreamReaderC# program that uses text properties
using System;
using System.IO;
class Program
{
static void Main()
{
// This file will have a new line at the end.
FileInfo info = new FileInfo("C:\\file.txt");
using (StreamWriter writer = info.AppendText())
{
writer.WriteLine("Line");
}
// This file will have the word New in it.
info = new FileInfo("C:\\new.txt");
using (StreamWriter writer = info.CreateText())
{
writer.WriteLine("New");
}
// Get a StreamReader with OpenText.
using (StreamReader reader = info.OpenText())
{
Console.WriteLine(reader.ReadToEnd());
}
}
}
Output
New
Security methods
GetAccessControl
SetAccessControl
GetLifetimeService
InitializeLifetimeService
Error caused by using Encrypt when unsupported
Unhandled Exception: System.IO.IOException: The request is not supported.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.File.Encrypt(String path)
at System.IO.FileInfo.Encrypt()
at Program.Main() in C:\...Program.cs:line 10