Csharp/C Sharp/File Stream/File Attributes
Check file Attribute
using System;
using System.IO;
class MainClass {
static void Main() {
FileInfo file = new FileInfo("data.txt");
Console.WriteLine(file.Attributes.ToString());
if (file.Attributes == FileAttributes.ReadOnly) {
Console.WriteLine("File is read-only (faulty test).");
}
if ((file.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly) {
Console.WriteLine("File is read-only (correct test).");
}
}
}
Get file attribute: ReadOnly
using System;
using System.IO;
static class Test
{
static void Main()
{
FileInfo file = new FileInfo("test.cs");
Console.WriteLine(file.Attributes.ToString());
if (file.Attributes == FileAttributes.ReadOnly)
{
Console.WriteLine("File is read-only (faulty test).");
}
if ((file.Attributes & FileAttributes.ReadOnly) ==
FileAttributes.ReadOnly)
{
Console.WriteLine("File is read-only (correct test).");
}
}
}