2017-01-27 7 views
1

私はディレクトリ内のファイルを見つけてリストボックスにリストするプログラムを持っていますが、私が使用している次のコードは見つかったファイルの絶対パスを追加します。ファイル名のみを一覧表示するには?

完全なパスではなくファイル名だけを追加するために欠けているものがありますか?私はここでしか代わりListBox.Items.Add(FoundFile)

+2

*仮定VB.Net *:[Path.GetFileName()](https://msdn.microsoft.com/en-us/library/system.io.path.getfilename(V = vs.110 ).aspx) –

+1

直接関係はありませんが、 'My.Computer.FileSystem.CurrentDirectory&" \ "&Details.IDL.Text'を3回繰り返す場所がわかりますか?それを変数に入れる方が良いでしょう。また、[Path.Combine](https://msdn.microsoft.com/en-us/library/system.io.path.combine(v = vs.110).aspx)を使用してパスの区切り文字を心配する必要はありません。 –

答えて

1

ListBox.Items.Add(IO.Path.GetFileName(FoundFile))を入れていた、それを修正する

If My.Computer.FileSystem.DirectoryExists(My.Computer.FileSystem.CurrentDirectory & "\" & Details.IDL.Text) Then 
    For Each FoundFile As String In My.Computer.FileSystem.GetFiles(My.Computer.FileSystem.CurrentDirectory & "\" & Details.IDL.Text) 
     ListBox.Items.Add(FoundFile) 
    Next 
Else 
    My.Computer.FileSystem.CreateDirectory(My.Computer.FileSystem.CurrentDirectory & "\" & Details.IDL.Text) 
End If 

あなたがGetFileNameを使用している方法と一緒に、GetFileNameWithoutExtensionで個別にファイル名をリストする実施例です。

Dim fileName As String = "C:\mydir\myfile.ext" 
Dim pathname As String = "C:\mydir\" 
Dim result As String 

result = Path.GetFileNameWithoutExtension(fileName) 
Console.WriteLine("GetFileNameWithoutExtension('{0}') returns '{1}'", fileName, result) 

result = Path.GetFileName(pathname) 
Console.WriteLine("GetFileName('{0}') returns '{1}'", pathname, result) 
関連する問題