#author("2019-11-29T21:23:09+09:00","","")
#author("2019-11-29T21:25:35+09:00","","")
#navi(../)
* ファイルの有無を確認する・System.IO.File.Exists [#ja8254b7]
「ファイルが存在しているかを調べたい」~
このような時は、System.IO.File.Exists を使えば簡単にファイルの存在チェックが可能です。~
以下にC#とVisual Basic(VB)のサンプルコードを公開します。~
&color(red){''ファイル''なの?それとも''ディレクトリ''なの?を調べたい場合は、以下のリンク記事を参考にしてください。};~
[[ファイルなの?ディレクトリなの?の判別方法>.NET/ファイルなの?ディレクトリなの?の判別]]

#contents
#htmlinsert(windev-top.html)

*参考サイト [#o7d23a8d]
-[[Microsoft|.NET File.Exists(String) メソッド>https://docs.microsoft.com/ja-jp/dotnet/api/system.io.file.exists?view=netframework-4.8]]


*関連記事 [#m664447a]
-[[ディレクトリの有無を確認する>.NET/ディレクトリの有無を確認する]]
-[[ファイルなの?ディレクトリなの?の判別方法>.NET/ファイルなの?ディレクトリなの?の判別]]

* File.Exists(String)について [#g9ff3583]
File.Exists(String)にファイル名を指定することにより、ファイルの存在確認ができます。~
ファイルが存在すれば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)

トップ   編集 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS