gpt4 book ai didi

vb.net - 如何在VB中获取文件的文件名?

转载 作者:行者123 更新时间:2023-12-03 16:00:00 25 4
gpt4 key购买 nike

我制作了一个搜索程序,用于在计算机中搜索文件列表,然后将文件复制到存储文件夹中。文件名可以是“*11*2.txt”,只要程序找到这个模式,就应该复制到store文件夹中。问题是我在搜索之前不知道文件的确切名称,我不想重命名文件,我不知道如何保存文件。请帮忙

我使用以下内容来查找文件,它的工作

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

这是 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

最佳答案

首先,您应该检查是否ToPath是一个有效目录,因为它来自 TextBox :

Dim isValidDir = Directory.Exists(ToPath)

二、可以使用 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

(免责声明:从手机输入)

关于vb.net - 如何在VB中获取文件的文件名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12257388/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com