ファイルが存在しているかを確認する
System.IO.File.Exists()
File.Exists メソッド (String) (System.IO)
System.IO.File.Exists("C:\hoge.txt")
System.IO.File.Exists(@"C:\hoge.txt")
ディレクトリが存在しているかを確認する
System.IO.Directory.Exists()
Directory.Exists メソッド (String) (System.IO)
System.IO.Directory.Exists("C:\")
System.IO.Directory.Exists(@"C:\")
ディレクトリ内のファイルを取得する
System.IO.Directory.GetFiles()
Directory.GetFiles メソッド (System.IO)
'ファイル名を取得 System.IO.Directory.GetFiles("C:\") '条件を指定してファイル名を取得 System.IO.Directory.GetFiles("C:\", "c*") '配列の長さでディレクトリ内のファイル数を取得 System.IO.Directory.GetFiles("C:\").Length
//ファイル名を取得 System.IO.Directory.GetFiles(@"C:\") //条件を指定してファイル名を取得 System.IO.Directory.GetFiles(@"C:\", "c*") //配列の長さでディレクトリ内のファイル数を取得 System.IO.Directory.GetFiles(@"C:\").Length
ファイルを作成する
System.IO.File.Create()
Dim fStream As System.IO.FileStream = Nothing Try 'ファイルを作成してFileStreamを取得 fStream = System.IO.File.Create("C:\hoge.txt") Catch ex As Exception MessageBox.Show(ex.Message) Finally 'Nothing以外は必ずCloseして破棄 If Not fStream Is Nothing Then fStream.Close() fStream.Dispose() End If End Try
try { //FileStreamを取得 using (System.IO.FileStream fStream = System.IO.File.Create(@"C:\hoge.txt")) { // 必ずClose if (fStream != null) { fStream.Close(); } } } catch (Exception ex) { MessageBox.Show(ex.Message); }
ファイルに書き込みをする
System.IO.StreamWriter()
Dim wStream As System.IO.'ファイル名を取得 System.IO.Directory.GetFiles(@"C:\") '条件を指定してファイル名を取得 System.IO.Directory.GetFiles(@"C:\", "c*"); '配列の長さでディレクトリ内のファイル数を取得 System.IO.Directory.GetFiles(@"C:\").Length()= Nothing Try '第二引数は上書きの有無 True:上書きする False:上書きしない wStream = New System.IO.StreamWriter("C:\hoge.txt", _ False, _ System.Text.Encoding.Default) wStream.Write("fuga") Catch ex As Exception MessageBox.Show(ex.Message) Finally 'Nothing以外は必ずCloseして破棄 If Not wStream Is Nothing Then wStream .Close() wStream .Dispose() End If End Try
try { //Streamを取得 //第二引数は上書きの有無 True:上書きする False:上書きしない using (System.IO.StreamWriter wStream = new System.IO.StreamWriter(@"C:\hoge.txt", false, System.Text.Encoding.Default)) { wStream.Write("fuga"); // 必ずClose if (wStream != null) { wStream.Close(); } } } catch (Exception ex) { MessageBox.Show(ex.Message); }