永发信息网

vb里怎么判断指定目录下的指定文件名是否存在?

答案:3  悬赏:70  手机版
解决时间 2021-02-11 01:49
  • 提问者网友:我的未来我做主
  • 2021-02-10 04:04
vb里怎么判断指定目录下的指定文件名是否存在?比如在“E:\新建文件夹”下判断是否存在?求代码,如果需要什么控件说明一下,还有,如果存在的和不存在的条件
最佳答案
  • 五星知识达人网友:轻雾山林
  • 2021-02-10 04:45
If Dir("e:\新建文件夹\123.txt")<>"" Then
'文件存在
Else
'文件不存在
End If
全部回答
  • 1楼网友:神也偏爱
  • 2021-02-10 06:35
  • 方法1 调用函数GetFileAttributes() 检查返回值通过属性判断文件是否存在    DWORD WINAPI GetFileAttributes(  __in          LPCTSTR lpFileName )Return Value: If the function succeeds, the return value contains the attributes of the specified file or directory. If the function fails, the return value is INVALID_FILE_ATTRIBUTES. To get extended error information, call GetLastError. The attributes can be one or more of the following values. 以上英文来源微软MSDN, 如果失败返回 INVALID_FILE_ATTRIBUTES, 那就说明文件不存在;
  • 方法2 调用函数fopen() 如果文件不存在,调用函数会返回空;     FILE *fopen(         const char *filename,        const char *mode );Return Value: Each of these functions returns a pointer to the open file. A null pointer value indicates an error. If filename or mode is NULL or an empty string, these functions trigger the invalid parameter handler, as described in Parameter Validation. If execution is allowed to continue, these functions return NULL and set errno to EINVAL. 上面是复制MSDN上的说明,如果文件不存在那么返回空指针;
    • 2楼网友:痴妹与他
    • 2021-02-10 06:15

    首先在窗体上添加一个列表框list1,它用来显示文件名,以"d:\123"这个文件夹为例子,程序如下,只需将路径换为你需要的路径即可,

    private sub form_load() dim fname as string dim student as string dim foldername as string

    foldername = "d:\123" '此处换为你指定的目录即可

    fname = dir(foldername & "\*.*", 7) list1.clear do if fname = "" then exit do list1.additem fname fname = dir() loop

    end sub

    我要举报
    如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
    点此我要举报以上问答信息
    大家都在看
    推荐资讯