C# ファイルの存在チェックを実行する

  • 作成日 2021.02.19
  • C#
C# ファイルの存在チェックを実行する

C#で、File.Existsを使用して、ファイルの存在チェックを実行するサンプルコードを記述してます。

環境

  • OS windows10 pro 64bit
  • Microsoft Visual Studio Community 2019 Version 16.7.1

File.Exists使い方

File.Existsを使用すると、指定したファイルが存在するか確認することが可能です。

以下は、「chrome.exe」が指定したパスに存在するかを確認するコードとなります。

using System;
using System.IO;

namespace testapp
{
    class Program
    {
        static void Main(string[] args)
        {

            string path = @"C:\Program Files\Google\Chrome\Application\chrome.exe";

            if (File.Exists(path))
            {
                Console.WriteLine("{0}は存在します", path);
                // C:\Program Files\Google\Chrome\Application\chrome.exeは存在します
            }
            else
            {
                Console.WriteLine("{0}は存在しません", path);
            }

        }
    }
}