Option Strict On Public Class Form1 'VisualBasic2008 'ファイル入門 'このプログラムは製作者しか使用できません '参照のみ '習得情報 'VBヘルプ(H)⇒カテゴリから検索⇒VisualBasic言語の習得⇒フォルダー内のファイル名の取得~テキストファイルの削除までのレッスン。 'Option Strict Onの場合(selectedItem.Todtringを入れてください) Dim FavoritePictures As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\FavoritePictures.txt" Private Sub LoadPictures_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoadPictures.Click Me.ListBox1.Items.Clear() Me.PictureBox1.ImageLocation = "" For Each foundImage As String In _ My.Computer.FileSystem.GetFiles( _ My.Computer.FileSystem.SpecialDirectories.MyPictures, _ FileIO.SearchOption.SearchTopLevelOnly, "*.gif*", "*.jpg*", "*.png*") Me.ListBox1.Items.Add(foundImage) Next 'ピクチャファルダにイメージがない場合は、ユーザーにMsgBoxを表示する If Me.ListBox1.Items.Count < 1 Then MsgBox("JPG & GIF ファイルがフォルダにありません") End If End Sub Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged Me.PictureBox1.ImageLocation = Me.ListBox1.SelectedItem.ToString End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FavoritesAdd.Click If PictureBox1.ImageLocation <> "" Then My.Computer.FileSystem.WriteAllText(FavoritePictures, Me.ListBox1.SelectedItem.ToString & ",", True) End If End Sub Private Sub LoadFavorites_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoadFavorites.Click Me.ListBox1.Items.Clear() Me.PictureBox1.ImageLocation = "" If My.Computer.FileSystem.FileExists(FavoritePictures) Then Dim Myreder As Microsoft.VisualBasic.FileIO.TextFieldParser Myreder = My.Computer.FileSystem.OpenTextFieldParser(FavoritePictures) Myreder.SetDelimiters(",") Dim textFields As String() = Myreder.ReadFields() For Each currentField As String In textFields If My.Computer.FileSystem.FileExists(currentField) Then Me.ListBox1.Items.Add(currentField) End If Next Myreder.Close() Else MsgBox("ファイルがありません") End If End Sub Private Sub DeleteFavorites_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteFavorites.Click If My.Computer.FileSystem.FileExists(FavoritePictures) Then If MsgBox("ファイルを削除します" & "消去", MsgBoxStyle.YesNo, "Delete Favorite Pictures") = MsgBoxResult.Yes Then Me.ListBox1.Items.Clear() Me.PictureBox1.ImageLocation = "" My.Computer.FileSystem.DeleteFile(FavoritePictures) End If Else MsgBox("キャンセル") End If End Sub End Class