VB.NET, C#, PowerShellを使用しているエンジニアのためのサイト。
基本的な使用方法から開発で役立つTipsまで幅広く取り扱っています。
ファイルの属性を取得するにはFileクラスのGetAttributesメソッドを使用します。
ファイルの属性を設定するにはFileクラスのSetAttributesメソッドを使用します。
string filepath = @"C:\Work\Test.txt"; System.IO.FileAttributes FileAttr = new System.IO.FileAttributes(); FileAttr = System.IO.File.GetAttributes(filepath); if ((FileAttr & System.IO.FileAttributes.Compressed) == System.IO.FileAttributes.Compressed) Console.WriteLine("ファイルは圧縮されています"); if ((FileAttr & System.IO.FileAttributes.Directory) == System.IO.FileAttributes.Directory) Console.WriteLine("このファイルはディレクトリです"); if ((FileAttr & System.IO.FileAttributes.Encrypted) == System.IO.FileAttributes.Encrypted) Console.WriteLine("このファイルは暗号化されています"); if ((FileAttr & System.IO.FileAttributes.Hidden) == System.IO.FileAttributes.Hidden) Console.WriteLine("このファイルは隠しファイルです"); if ((FileAttr & System.IO.FileAttributes.ReadOnly) == System.IO.FileAttributes.ReadOnly) Console.WriteLine("このファイルは読み取り専用です"); if ((FileAttr & System.IO.FileAttributes.System) == System.IO.FileAttributes.System) Console.WriteLine("このファイルはシステムファイルです"); if ((FileAttr & System.IO.FileAttributes.Temporary) == System.IO.FileAttributes.Temporary) Console.WriteLine("このファイルは一時ファイルです");
string filepath = @"C:\Work\Test.txt"; //読み取り属性をセットする System.IO.File.SetAttributes(filepath, System.IO.File.GetAttributes(filepath) | System.IO.FileAttributes.ReadOnly);