HIRO's.NET

VB.NET, C#, PowerShell Tipsサイト

 VB.NET, C#, PowerShellを使用しているエンジニアのためのサイト。

 基本的な使用方法から開発で役立つTipsまで幅広く取り扱っています。

HIRO's.NET RSSHIRO's.NET RSS


C# 2005の開発でお困りのことはありませんか?
そんな悩みは当サイトで解決!!

HOME > C# 2005 Tips > ファイル操作 Tips メニュー

13.ファイルの属性を取得/設定する

UPDATE:2006/07/15 

<< 前のTips  次のTips >>

 


 ファイルの属性を取得するにはFileクラスのGetAttributesメソッドを使用します。
 ファイルの属性を設定するにはFileクラスのSetAttributesメソッドを使用します。

 
サンプル1
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("このファイルは一時ファイルです");


 
サンプル2
string filepath = @"C:\Work\Test.txt";

//読み取り属性をセットする
System.IO.File.SetAttributes(filepath, System.IO.File.GetAttributes(filepath) | 
    System.IO.FileAttributes.ReadOnly);