ちょっと生きづらさを抱えた人へ、気持ちを楽にするためのお手紙です。

【C#】ファイルの存在チェック

C# でファイルの存在確認。

【C#】 [ファイルを開く] ダイアログで選択したファイルのパスを取得
の続きなので、余分なコードが入っていますが、
System.IO.File.Exists() で、
パラメータにファイルパスを指定して、
trueが返却されたら存在している、
falseが返却されたら存在していない、です。
 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace CreateSQL
{
    public partial class Form1 : Form
    {
        private const string applicationName = "SQL Creator";
        
        public Form1()
        {
            InitializeComponent();
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            if(openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                TextInputPath.Text = openFileDialog1.FileName;
            }

        }

        private void TextInputPath_Leave(object sender, EventArgs e)
        {
            if (!File.Exists(TextInputPath.Text))
            {
                MessageBox.Show("ファイルが存在しません", applicationName);
            }
        }
    }
}

 

参考:File.Exists メソッド (String)

コメントを残す

メールアドレスが公開されることはありません。

CAPTCHA