VB.netリストボックスで選択した内容に合わせて2つのディレクトリを作成する。
単にディレクトリを作成するだけのVBコード
すぐに使い方を忘れるのでメモ
シンプルなクラスを使って分岐用のディレクトリパスに使用している。
構造体を使用したら、面倒なので使わないほうがよいとネットに書いてあってクラスを使うことにした。
iOSではクラスを使うとメモリを消費すると言われてVBで構造体にしたけど、エラーが出て型を合わせるのが面倒だった。
使い方は
リストボックスを選択する。
次にテキストボックスにディレクトリ名を入力する。
そして作成ボタンをクリックすると、
選択されたリストボックスによりどのディレクトリパスかが選択されて、テキストボックスのディレクトリ名が作成される。
2つのディレクトリを作成する必要があったのは、エクセルファイルと他のファイルでディレクトリを分けたかったから。
別に3つでも良いし1つでもよかった。
'VB.netリストボックスで選択した内容に合わせて2つのディレクトリを作成する。 ’決め打ちのディレクトリ ’VB.net環境 Public Class cDirPath Public DirPath1 As String Public DirPath2 As String Public Sub New() End Sub Public Sub New(iFirstPath As String, iSecondPath As String) DirPath1 = iFirstPath DirPath2 = iSecondPath End Sub End Class 'Form1のクラスに別のクラスを書くとデザインビューが表示されなくなるのでこの上のcDirPathクラスは新規ファイルを追加して置くこと。 Public Class Form1 Dim classStruct(3) As cDirPath ' = New cDirPath Dim arrayDir '配列、作成する分類を入れる 'ここでarrayDirに値を与えられない。宣言だけ 'Dim arrayDirPath, arrayDirPath2 'パス Public Structure DirStruct '構造体は面倒なのでVBでは使わない。 Public DirPath1 As String Public DirPath2 As String End Structure Dim structDirPath '= New DirStruct() {} '構造体ならこのカッコが重要 'クラスを配列として使う Private Enum Bunruitype 'enum列挙型 このアプリでは使っていないけど参考に入れた SY fac lili con pro End Enum Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load ListBox1.Items.Clear() arrayDir = New ArrayList() arrayDir.Add("SY") arrayDir.Add("ファリー") arrayDir.Add("リリ") Dim testString As String 'Dim tmpStruct As DirStruct '一旦別の構造体を使う。 'クラスオブジェクトを作成して2つのディレクトリパス与える classStruct(0) = New cDirPath("c:\vbdata\sy\", "c:\vbdata\subdata\sy\") classStruct(1) = New cDirPath("c:\vbdata\fa\", "c:\vbdata\subdata\fa\") classStruct(2) = New cDirPath("c:\vbdata\lili\", "c:\vbdata\subdata\lili\") '構造体を使うよりクラスを使えということらしい ' Dim structDirPath 'arrayDirPath.Add("") '個別のディレクトリのパス 構造体のほうがいいか 'arrayDirPath2.Add("") 'structDirPath = New DirStruct(5) {} 'このカッコが重要で数を指定 'testString = "test" ListBox1.Items.Add(arrayDir(0)) '配列から文字列を渡す ListBox1.Items.Add(arrayDir(1)) ListBox1.Items.Add(arrayDir(2)) 'select line 1 ListBox1.SelectedIndex = 0 End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim fname '= TextBox1.Text Dim fname2 Dim makeDir As String Dim makeDir2 As String Dim selectName fname = "" makeDir2 = "" If TextBox1.Text = "" Then MsgBox("フォルダー名が空っぽです") Return End If If ListBox1.SelectedIndex = -1 Then MsgBox("何も選択されていません") Return Else 'リストボックスで選択した内容でディレクトリパスを配列から与える。 ’テキスト文字列のチェックをしていない。 selectName = ListBox1.SelectedItem Select Case selectName Case arrayDir(0) '"SY" 'クラスに入れたディレクトリパスを取得 makeDir = classStruct(0).DirPath1 & TextBox1.Text makeDir2 = classStruct(0).DirPath2 & TextBox1.Text Case arrayDir(1) '"ファリー" makeDir = classStruct(1).DirPath1 & TextBox1.Text makeDir2 = classStruct(1).DirPath2 & TextBox1.Text Case arrayDir(2) '"リリー" makeDir = classStruct(2).DirPath1 & TextBox1.Text makeDir2 = classStruct(2).DirPath2 & TextBox1.Text End Select fname = makeDir fname2 = makeDir2 End If 'fname = TextBox1.Text If fname = "" Then MsgBox("空欄です") Return End If ’ディレクトリ作成、存在チェックしてない。 System.IO.Directory.CreateDirectory(fname) System.IO.Directory.CreateDirectory(fname2) MsgBox(fname.ToString & "作成しました") MsgBox(fname2.ToString & "作成しました") End Sub End Class