- 追加された行はこの色です。
- 削除された行はこの色です。
#author("2019-11-27T21:41:03+09:00","","")
#navi(../)
* ファイルの有無を確認する・System.IO.File.Exists [#ja8254b7]
「ファイルが存在しているかを調べたい」~
このような時は、System.IO.File.Exists を使えば簡単にファイルの存在チェックが可能です。~
以下にC#とVisual Basic(VB)のサンプルコードを公開します。~
&color(red){''ファイル''なの?それとも''ディレクトリ''なの?を調べたい場合は、~
''System.IO.Directory.Exists''を使えば簡単に判別できます。~
以下のリンク記事を参考にしてください。
-[[ファイルなの?ディレクトリなの?の判別方法>.NET/ファイルなの?ディレクトリなの?の判別]]
#contents
*参考サイト [#o7d23a8d]
-[[Microsoft|.NET File.Exists(String) メソッド>https://docs.microsoft.com/ja-jp/dotnet/api/system.io.file.exists?view=netframework-4.8]]
#htmlinsert(windev-top.html)
*関連記事 [#m664447a]
-[[ファイルなの?ディレクトリなの?の判別方法>.NET/ファイルなの?ディレクトリなの?の判別]]
* File.Exists(String) [#g9ff3583]
File.Exists(ファイル)を指定することにより、ファイルの存在確認ができます。~
存在すればtrue、存在しなければfalseが返却されます。
* File.Existsのサンプルコード [#w3e0813f]
以下は C# と Visual Basic(VB) File.Existsを使ったサンプルコードになります。
** C#のサンプルコード [#c282e580]
#ref(Program.cs)
using System;
namespace FileExistsCS
{
class Program
{
static void Main(string[] args)
{
String file = @"C:\Windows\System32\drivers\etc\hosts";
if (System.IO.File.Exists(file))
{
Console.WriteLine(file + " file found!");
}
else
{
Console.WriteLine(file + " file not found!");
}
}
}
}
** Visual Basic(VB)のサンプルコード [#va401cc7]
#ref(Module1.vb)
Module Module1
Sub Main()
Dim file As String = "C:\Windows\System32\drivers\etc\hosts"
If System.IO.File.Exists(file) Then
Console.WriteLine(file & " file found!")
Else
Console.WriteLine(file & " file not found!")
End If
End Sub
End Module
** 実行結果 [#vce085e4]
以下、サンプルコードを実行した時の出力になります。
#ref(01.png)
以上、File.Existsを使ったサンプルコードのご紹介でした。
#htmlinsert(windev-btm.html)