HIRO's.NET

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

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

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

HIRO's.NET RSSHIRO's.NET RSS


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

HOME > VB.NET 2005 Tips > ファイル操作 Tips メニュー

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

UPDATE:2006/07/15 

<< 前のTips  次のTips >>

 


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

 
サンプル1
Dim filepath As String = "C:\Work\Test.txt"
Dim FileAttr As New System.IO.FileAttributes

FileAttr = System.IO.File.GetAttributes(filepath)

If (FileAttr And IO.FileAttributes.Compressed) = IO.FileAttributes.Compressed Then
    Console.WriteLine("ファイルは圧縮されています")
End If

If (FileAttr And IO.FileAttributes.Directory) = IO.FileAttributes.Directory Then
    Console.WriteLine("このファイルはディレクトリです")
End If

If (FileAttr And IO.FileAttributes.Encrypted) = IO.FileAttributes.Encrypted Then
    Console.WriteLine("このファイルは暗号化されています")
End If

If (FileAttr And IO.FileAttributes.Hidden) = IO.FileAttributes.Hidden Then
    Console.WriteLine("このファイルは隠しファイルです")
End If

If (FileAttr And IO.FileAttributes.ReadOnly) = IO.FileAttributes.ReadOnly Then
    Console.WriteLine("このファイルは読み取り専用です")
End If

If (FileAttr And IO.FileAttributes.System) = IO.FileAttributes.System Then
    Console.WriteLine("このファイルはシステムファイルです")
End If

If (FileAttr And IO.FileAttributes.Temporary) = IO.FileAttributes.Temporary Then
    Console.WriteLine("このファイルは一時ファイルです")
End If


 
サンプル2
Dim filepath As String = "C:\Work\Test.txt"

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