vb.net 如何在VB中获取文件的文件名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12257388/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the file name of a file in VB?
提问by Marco
I make a search program for searching a list of files in a computer and then copy the file into a store folder. The file name could be "*11*2.txt" As long as the program find this pattern, it should copy to the store folder. The problem is that I don't know the exactly name of the file before the search and I don't want to rename the file, I don't know how to save the file. Please help
我制作了一个搜索程序,用于在计算机中搜索文件列表,然后将文件复制到存储文件夹中。文件名可以是“*11*2.txt”,只要程序找到这个模式,就应该复制到store文件夹中。问题是我在搜索之前不知道文件的确切名称,我不想重命名文件,我不知道如何保存文件。请帮忙
I use the following to find the file, which does its work
我使用以下内容来查找文件,它的工作
Public Sub DirSearch(ByVal sDir As String, ByVal FileName As String)
Dim To_Path As String
To_Path = Form1.TextBox5.Text
For Each foundFile As String In My.Computer.FileSystem.GetFiles(sDir, FileIO.SearchOption.SearchAllSubDirectories, FileName)
Copy2Local(foundFile, To_Path)
Next
End Sub
Here is the current version of the Copy2Local (Note: it is not working right)
这是 Copy2Local 的当前版本(注意:它不能正常工作)
Public Sub Copy2Local(ByVal Copy_From_Path As String, ByVal Copy_To_Path As String)
' Specify the directories you want to manipulate.
Try
Dim fs As FileStream = File.Create(Copy_From_Path)
fs.Close()
' Copy the file.
File.Copy(Copy_From_Path, Copy_To_Path)
Catch
End Try
End Sub
回答by Tim Schmelter
First, you should check if ToPath
is a valid directory since it's coming from a TextBox
:
首先,您应该检查是否ToPath
是有效目录,因为它来自TextBox
:
Dim isValidDir = Directory.Exists(ToPath)
Second, you can use Path.Combine
to create a path from separate (sub)directories or file-names:
其次,您可以使用Path.Combine
从单独的(子)目录或文件名创建路径:
Dim copyToDir = Path.GetDirectoryName(Copy_To_Path)
Dim file = Path.GetFileName(Copy_From_Path)
Dim newPath = Path.Combine(copyToDir, file)
http://msdn.microsoft.com/en-us/library/system.io.path.aspx
http://msdn.microsoft.com/en-us/library/system.io.path.aspx
(disclaimer: typed from a mobile)
(免责声明:从手机输入)
回答by Heinzi
To answer your question: You can get the file name with Path.GetFileName. Example:
回答您的问题:您可以使用Path.GetFileName获取文件名。例子:
Dim fileName As String = Path.GetFileName(foundFile)
However, there's a bunch of other things wrong with your code:
但是,您的代码还有很多其他问题:
Here,
Dim fs As FileStream = File.Create(Copy_From_Path) fs.Close()
you are overwritingyour source file. This does not seem like a good idea. ;-)
And here,
Try ... Catch ' Do Nothing End Try
You are throwing away exceptions that would help you find and diagnose problems. Don't do that. It makes debugging a nightmare.
这里,
Dim fs As FileStream = File.Create(Copy_From_Path) fs.Close()
你正在覆盖你的源文件。这似乎不是一个好主意。;-)
和这里,
Try ... Catch ' Do Nothing End Try
您正在丢弃可以帮助您查找和诊断问题的异常。不要那样做。它使调试成为一场噩梦。
回答by shehan sachintha
In vb.net, I'm using the following code to find the filename
在 vb.net 中,我使用以下代码来查找文件名
Textbox1.Text = New FileInfo(OpenFileDialog.FileName).Name
this code work fine with open file dialog box
此代码在打开文件对话框中工作正常