Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 如何在单击按钮时从文本框验证文件名?图书管理系统_Vb.net_Visual Studio_Validation_Textbox - Fatal编程技术网

Vb.net 如何在单击按钮时从文本框验证文件名?图书管理系统

Vb.net 如何在单击按钮时从文本框验证文件名?图书管理系统,vb.net,visual-studio,validation,textbox,Vb.net,Visual Studio,Validation,Textbox,我想知道如何检查从textbox插入的文件名是否与程序本身位于同一文件夹中 例如,我将在文本框中输入notepad.exe,然后当您单击按钮时,它将检查程序所在文件夹中是否存在该程序。如果我在文本框中输入notepad.ex,那么它没有找到任何具有该名称的文件,那么它将给出错误。我必须使用此代码来检查该文件是否存在 Dim currentPath As String = System.IO.Path.Combine(IO.Directory.GetCurrentDirectory(), Text

我想知道如何检查从textbox插入的文件名是否与程序本身位于同一文件夹中


例如,我将在文本框中输入notepad.exe,然后当您单击按钮时,它将检查程序所在文件夹中是否存在该程序。如果我在文本框中输入notepad.ex,那么它没有找到任何具有该名称的文件,那么它将给出错误。

我必须使用此代码来检查该文件是否存在

Dim currentPath As String = System.IO.Path.Combine(IO.Directory.GetCurrentDirectory(), Textbox1.Text)

    If IO.File.Exists(currentPath) Then
        ..Do something here
    Else
        MsgBox("Executable doesn't exist!", vbOKOnly, "Error!")
    End If
只需在文件名的开头添加一个“\”,即可检查当前目录路径,该路径始终与主程序集所在的目录相同:

Dim filename As String = TextBox1.Text

If Not File.Exists(String.Format(".\{0}", filename)) Then
    MessageBox.Show("File not found.", "", MessageBoxButtons.OK, MessageBoxIcon.Error)

Else
    ' File found.

End If

我建议仅当您不确定工作目录是什么时才使用
Directory.GetCurrentDirectory()
,例如,当使用
Directory.SetCurrentDirectory()
方法更改默认工作目录路径时。

不要将问题部分作为答案发布。编辑您的问题我正在使用Directory.GetCurrentDirectory(),因为我正在制作的程序并非每次都位于同一文件夹或路径。但是谢谢你的回答。