.NET/ファイルなの?ディレクトリなの?の判別
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
検索
|
最終更新
|
ヘルプ
]
開始行:
#navi(../)
* ファイルなの?ディレクトリなの?の判別方法 [#ecc18483]
System.IO.File.Exists、System.IO.Directory.Existsを使って...
判別することが可能ですが、本記事では、''System.IO.File.Ge...
ファイルの各種属性(隠しファイル、リードオンリーファイルな...
普通のファイル属性(System.IO.FileAttributes.Archive、Syst...
ファイルorディレクトリの判別を行うサンプルコードになりま...
尚、ファイルやディレクトリが存在しないと、例外が発生する...
&color(red){細かなファイル属性(FileAttributes)までは対応...
#htmlinsert(windev-top.html)
#contents
* 参考サイト [#l212866d]
-[[Microsoft|.NET File.GetAttributes(String) メソッド>htt...
-[[Microsoft|.NET FileAttributes 列挙型>https://docs.micr...
* 関連記事 [#lcd8befb]
-[[ファイルの有無を確認する>.NET/ファイルの有無を確認する]]
-[[ディレクトリの有無を確認する>.NET/ディレクトリの有無を...
* File.GetAttributes(String)メソッド [#i1e3d908]
File.GetAttributes(String)メソッドの引数にある、Stringに...
指定したファイル(ディレクトリ)が存在した場合、FileAttribu...
*サンプルコード [#t4ee222f]
File.GetAttributes、FileAttributesを使って、ファイルなの...
尚、指定したファイル、ディレクトリが存在しない場合、GetAt...
*C#サンプルコード [#iefd67a5]
//#ref(Program.cs)
using System;
namespace GetAttrCS
{
class Program
{
static void Main(string[] args)
{
String file = @"C:\Windows\System32\drivers\...
String dir = @"C:\Windows";
String notExist = @"C:\path\to";
Console.WriteLine(file + ":" + GetAttr(file));
Console.WriteLine(dir + ":" + GetAttr(dir));
Console.WriteLine(notExist + ":" + GetAttr(n...
}
static String GetAttr(String pathto)
{
String result = "";
System.IO.FileAttributes attr ;
try
{
attr = System.IO.File.GetAttributes(path...
if ((attr & System.IO.FileAttributes.Dir...
{
result = "Directory";
}
else if((attr & System.IO.FileAttributes...
{
result = "Archive file";
}
else if ((attr & System.IO.FileAttribute...
{
result = "Normal file";
}
else
{
result = "unknown";
}
}
catch (Exception)
{
result = "error";
}
return result;
}
}
}
*Visual Basic(VB)サンプルコード [#q22e5c12]
//#ref(Module1.vb)
Module Module1
Sub Main()
Dim file As String = "C:\Windows\System32\driver...
Dim dir As String = "C:\Windows"
Dim notExist As String = "C:\path\to"
Console.WriteLine(file & ":" & GetAttr(file))
Console.WriteLine(dir & ":" & GetAttr(dir))
Console.WriteLine(notExist & ":" & GetAttr(notEx...
End Sub
Function GetAttr(pathto As String) As String
Dim result As String = ""
Dim attr As System.IO.FileAttributes
Try
attr = System.IO.File.GetAttributes(pathto)
If ((attr And System.IO.FileAttributes.Direc...
result = "Directory"
ElseIf ((attr And System.IO.FileAttributes.A...
result = "Archive file"
ElseIf ((attr And System.IO.FileAttributes.N...
result = "Normal file"
Else
result = "unknown"
End If
Catch ex As Exception
result = "error"
End Try
Return result
End Function
End Module
** 実行結果 [#ub693115]
実行結果のキャプチャになります。~
ファイル(System.IO.FileAttributes.Normal、System.IO.FileA...
#ref(01.png)
以上、System.IO.File.GetAttributesメソッドを使ってファイ...
#htmlinsert(windev-btm.html)
終了行:
#navi(../)
* ファイルなの?ディレクトリなの?の判別方法 [#ecc18483]
System.IO.File.Exists、System.IO.Directory.Existsを使って...
判別することが可能ですが、本記事では、''System.IO.File.Ge...
ファイルの各種属性(隠しファイル、リードオンリーファイルな...
普通のファイル属性(System.IO.FileAttributes.Archive、Syst...
ファイルorディレクトリの判別を行うサンプルコードになりま...
尚、ファイルやディレクトリが存在しないと、例外が発生する...
&color(red){細かなファイル属性(FileAttributes)までは対応...
#htmlinsert(windev-top.html)
#contents
* 参考サイト [#l212866d]
-[[Microsoft|.NET File.GetAttributes(String) メソッド>htt...
-[[Microsoft|.NET FileAttributes 列挙型>https://docs.micr...
* 関連記事 [#lcd8befb]
-[[ファイルの有無を確認する>.NET/ファイルの有無を確認する]]
-[[ディレクトリの有無を確認する>.NET/ディレクトリの有無を...
* File.GetAttributes(String)メソッド [#i1e3d908]
File.GetAttributes(String)メソッドの引数にある、Stringに...
指定したファイル(ディレクトリ)が存在した場合、FileAttribu...
*サンプルコード [#t4ee222f]
File.GetAttributes、FileAttributesを使って、ファイルなの...
尚、指定したファイル、ディレクトリが存在しない場合、GetAt...
*C#サンプルコード [#iefd67a5]
//#ref(Program.cs)
using System;
namespace GetAttrCS
{
class Program
{
static void Main(string[] args)
{
String file = @"C:\Windows\System32\drivers\...
String dir = @"C:\Windows";
String notExist = @"C:\path\to";
Console.WriteLine(file + ":" + GetAttr(file));
Console.WriteLine(dir + ":" + GetAttr(dir));
Console.WriteLine(notExist + ":" + GetAttr(n...
}
static String GetAttr(String pathto)
{
String result = "";
System.IO.FileAttributes attr ;
try
{
attr = System.IO.File.GetAttributes(path...
if ((attr & System.IO.FileAttributes.Dir...
{
result = "Directory";
}
else if((attr & System.IO.FileAttributes...
{
result = "Archive file";
}
else if ((attr & System.IO.FileAttribute...
{
result = "Normal file";
}
else
{
result = "unknown";
}
}
catch (Exception)
{
result = "error";
}
return result;
}
}
}
*Visual Basic(VB)サンプルコード [#q22e5c12]
//#ref(Module1.vb)
Module Module1
Sub Main()
Dim file As String = "C:\Windows\System32\driver...
Dim dir As String = "C:\Windows"
Dim notExist As String = "C:\path\to"
Console.WriteLine(file & ":" & GetAttr(file))
Console.WriteLine(dir & ":" & GetAttr(dir))
Console.WriteLine(notExist & ":" & GetAttr(notEx...
End Sub
Function GetAttr(pathto As String) As String
Dim result As String = ""
Dim attr As System.IO.FileAttributes
Try
attr = System.IO.File.GetAttributes(pathto)
If ((attr And System.IO.FileAttributes.Direc...
result = "Directory"
ElseIf ((attr And System.IO.FileAttributes.A...
result = "Archive file"
ElseIf ((attr And System.IO.FileAttributes.N...
result = "Normal file"
Else
result = "unknown"
End If
Catch ex As Exception
result = "error"
End Try
Return result
End Function
End Module
** 実行結果 [#ub693115]
実行結果のキャプチャになります。~
ファイル(System.IO.FileAttributes.Normal、System.IO.FileA...
#ref(01.png)
以上、System.IO.File.GetAttributesメソッドを使ってファイ...
#htmlinsert(windev-btm.html)
ページ名: