我是VB新手,请大家指点一下。
Dim fs As New FileSystemObject
fs.DeleteFile(label1.caption)
我将文件路径放到label1.caption中的。上面的代码应该怎么写啊,怎么每次提示都找不到路径呢??
还有其他方法吗??
答案对就结贴。

解决方案 »

  1.   

    你可以这样写:
        On Error Goto errOpen
        If fs.FileExists(label1.caption) = True Then
            fs.DeleteFile label1.caption, True
        End If
        MsgBox "文件已经成功删除!", vbExclamation, "删除提示"
        Exit sub
    errOpen:
        If Err.Number = 70 Then
             MsgBox "所选保存文件已经打开!请先关闭该文件!", vbExclamation, "删除提示"
        End If
        Exit sub
      

  2.   

    上面的代码有点小问题,更正为:
        On Error Goto errOpen
        If fs.FileExists(label1.caption) = True Then
            fs.DeleteFile label1.caption, True
            MsgBox "文件已经成功删除!", vbExclamation, "删除提示"
        Else
            MsgBox "文件删除失败,文件不存在!", vbExclamation, "删除提示"
        End If
        Exit sub
    errOpen:
        If Err.Number = 70 Then
             MsgBox "所选保存文件已经打开!请先关闭该文件!", vbExclamation, "删除提示"
        End If
        Exit sub
      

  3.   

    理论上你的代码没什么问题
    Label1.Caption = "d:\aa.txt"
    Dim fs As New FileSystemObject
    fs.DeleteFile (Label1.Caption)
    在我机器上运行通过!!需要注意两点:
    1、文件的后缀一定要加上
    2、你有没有在工程中包含microsoft scripting runtime