运行程序时找不到路径

时间:2013-07-10 01:12:01

标签: vb6

此代码用于更新数据库,但每次单击“开始更新”按钮时,都会显示“未找到路径”错误。

Dim strEmpFileName As String
Dim strBackSlash As String
Dim intEmpFileNbr As Integer
Dim strEmpFileName1 As String
Dim strBackSlash1 As String
Dim intEmpFileNbr1 As Integer
Dim fPath As New FileSystemObject
Dim strEmpFileName2 As String
Dim strBackSlash2 As String
Dim intEmpFileNbr2 As Integer

Dim strEmpFileName21 As String
Dim strBackSlash21 As String
Dim intEmpFileNbr21 As Integer

Dim strEmpFileName21X As String
Dim strBackSlash21X As String
Dim intEmpFileNbr21X As Integer

Dim strEmpFileName21s As String
Dim strBackSlash21s As String
Dim intEmpFileNbr21s As Integer


strBackSlash = IIf(Right$(App.Path, 1) = "\", "", "\")
strEmpFileName = App.Path & strBackSlash & "\SOURCE\SWA.exe"
txtSource.Text = strEmpFileName

FileCopy txtSource.Text, "\\Mainfile\SSMS_UPDATE\SHIPS ACCOUNTING\SWA.exe"
FileCopy txtSource.Text, "C:\SANKO PROGRAM\SPECIAL WORK\SWA.exe"

2 个答案:

答案 0 :(得分:1)

由于您已经在检查

中的反斜杠
strBackSlash = IIf(Right$(App.Path, 1) = "\", "", "\")
strEmpFileName = App.Path & strBackSlash & "\SOURCE\SWA.exe"

您不应该在"\SOURCE\SWA.exe"

的开头需要反斜杠

答案 1 :(得分:-1)

您正在滥用IIf功能。 语法,IIf(expr,truepart,falsepart)。您的语句检查反斜杠,如果最后一个字符是“\”,则将变量设置为空字符串。但是,如果变量不是路径中的最后一个字符,则false部分将变量设置为“\”。例如,如果App.Path = C:\ MyApplication ,您的IIF函数将设置strBackSlash =“\”而strEmpFileName将是 C:\ MyApplication \\ SOURCE \ SWA.exe 。对于您的代码,您希望使用常规 If 语句将反斜杠字符替换为空字符串,然后在构建路径时使用硬编码反斜杠。

strAppPath = App.Path
If(Right$(strAppPath, 1) = "\" Then
    strAppPath = Left$(strAppPath, Len(strAppPath) - 1)
End Id
strEmpFileName = strAppPath & "\SOURCE\SWA.exe"

完整MSDN documentation is here

此外,获取应用程序路径是很多事情。我建议您编写自己的函数来执行此操作并将其添加到项目.bas文件中。然后从任何需要的地方调用函数,返回的路径格式(带或不带尾部反斜杠)是一致的。我的个人功能确保我有一个尾随反斜杠。

Public Function AppPath() As String
    Dim sAppPath As String

    sAppPath = App.Path
    If Right$(sAppPath, 1) <> "\" Then  'check that I'm not in the root
        sAppPath = sAppPath & "\"
    End If

    AppPath = sAppPath

End Function

用法: strEmpFileName = AppPath() & "SOURCE\SWA.exe"