VC判断一个文件为目录的方法

 更新时间:2020年4月25日 17:39  点击:2191

本文实例讲述了VC判断一个文件为目录的方法,分享给大家供大家参考。具体实现方法如下:

这是一个自定义函数,用于判断一个文件是否为目录:

复制代码 代码如下:
/**
 * check whether a file is a directory
 @return true if is a directory, else false(if file not exists, false)
 */
__declspec(dllexport) bool IsDirectory(const char* filename)
{
 
  DWORD dwAttr = ::GetFileAttributes(filename);  //得到文件属性
 
  if (dwAttr == 0xFFFFFFFF)    // 文件或目录不存在
    return false;
  else if (dwAttr&FILE_ATTRIBUTE_DIRECTORY)  // 如果是目录
    return true;
  else
    return false;
}

以下是GetFileAttribute定义,摘自msdn:

Retrieves a set of FAT file system attributes for a specified file or directory.得到FAT文件系统的文件属性

Parameters

lpFileName

The name of the file or directory.In the ANSI version of this function, the name is limited to MAX_PATH characters. To extend this limit to 32,767 wide characters, call the Unicode version of the function and prepend "//?/" to the path. For more information, see Naming a File.文件名或目录名。最大长度为系统的文件名最大长度。如果是unicode环境,需要调用这个函数的unicode版本。

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.
如果函数失败,返回值是INVALID_FILE_ATTRIBUTES. 可以通过GetLastError获取更详细的出错信息
The attributes can be one or more of the following values.
文件属性可以是下列值的一个或多个的组合。

Return code/value Description

FILE_ATTRIBUTE_ARCHIVE
32
0x20

A file or directory that is an archive file or directory.

Applications use this attribute to mark files for backup or removal.

存档文件

FILE_ATTRIBUTE_COMPRESSED
2048
0x800

A file or directory that is compressed.

For a file, all of the data in the file is compressed.

For a directory, compression is the default for newly created files and subdirectories.

压缩文件

FILE_ATTRIBUTE_DEVICE
64
0x40

Reserved; do not use.

FILE_ATTRIBUTE_DIRECTORY
16
0x10

The handle that identifies a directory.

目录文件

FILE_ATTRIBUTE_ENCRYPTED
16384
0x4000

A file or directory that is encrypted.

For a file, all data streams in the file are encrypted.

For a directory, encryption is the default for newly created files and subdirectories.

加密文件

FILE_ATTRIBUTE_HIDDEN
2
0x2

The file or directory is hidden. It is not included in an ordinary directory listing.

隐藏文件

FILE_ATTRIBUTE_NORMAL
128
0x80

A file or directory that does not have other attributes set.

This attribute is valid only when used alone.

 

FILE_ATTRIBUTE_NOT_CONTENT_INDEXED
8192
0x2000

The file is not to be indexed by the content indexing service.

FILE_ATTRIBUTE_OFFLINE
4096
0x1000

The data of a file is not available immediately.

This attribute indicates that the file data is physically moved to offline storage. This attribute is used by Remote Storage, which is the hierarchical storage management software. Applications should not arbitrarily change this attribute.

FILE_ATTRIBUTE_READONLY
1
0x1

A file or directory that is read-only.

For a file, applications can read the file, but cannot write to it or delete it.

For a directory, applications cannot delete it.

FILE_ATTRIBUTE_REPARSE_POINT
1024
0x400

A file or directory that has an associated reparse point, or a file that is a symbolic link.

FILE_ATTRIBUTE_SPARSE_FILE
512
0x200

A file that is a sparse file.

FILE_ATTRIBUTE_SYSTEM
4
0x4

A file or directory that the operating system uses a part of, or uses exclusively.

FILE_ATTRIBUTE_TEMPORARY
256
0x100

A file that is being used for temporary storage.

File systems avoid writing data back to mass storage if sufficient cache memory is available, because typically, an application deletes a temporary file after the handle is closed. In that scenario, the system can entirely avoid writing the data. Otherwise, the data is written after the handle is closed.

FILE_ATTRIBUTE_VIRTUAL
65536
0x10000

A file is a virtual file.


希望本文所述对大家的VC程序设计有所帮助。

[!--infotagslink--]

相关文章

  • php读取zip文件(删除文件,提取文件,增加文件)实例

    下面小编来给大家演示几个php操作zip文件的实例,我们可以读取zip包中指定文件与删除zip包中指定文件,下面来给大这介绍一下。 从zip压缩文件中提取文件 代...2016-11-25
  • Jupyter Notebook读取csv文件出现的问题及解决

    这篇文章主要介绍了JupyterNotebook读取csv文件出现的问题及解决,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2023-01-06
  • JavaScript判断浏览器及其版本信息

    本篇文章主要分享了通过window.navigator来判断浏览器及其版本信息的实例代码。具有一定的参考价值,下面跟着小编一起来看下吧...2017-01-23
  • 解决Pycharm的项目目录突然消失的问题

    今天小编就为大家分享一篇解决Pycharm的项目目录突然消失的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-04-22
  • Photoshop打开PSD文件空白怎么解决

    有时我们接受或下载到的PSD文件打开是空白的,那么我们要如何来解决这个 问题了,下面一聚教程小伙伴就为各位介绍Photoshop打开PSD文件空白解决办法。 1、如我们打开...2016-09-14
  • C#操作本地文件及保存文件到数据库的基本方法总结

    C#使用System.IO中的文件操作方法在Windows系统中处理本地文件相当顺手,这里我们还总结了在Oracle中保存文件的方法,嗯,接下来就来看看整理的C#操作本地文件及保存文件到数据库的基本方法总结...2020-06-25
  • 解决python 使用openpyxl读写大文件的坑

    这篇文章主要介绍了解决python 使用openpyxl读写大文件的坑,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-13
  • C#实现HTTP下载文件的方法

    这篇文章主要介绍了C#实现HTTP下载文件的方法,包括了HTTP通信的创建、本地文件的写入等,非常具有实用价值,需要的朋友可以参考下...2020-06-25
  • SpringBoot实现excel文件生成和下载

    这篇文章主要为大家详细介绍了SpringBoot实现excel文件生成和下载,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-02-09
  • php无刷新利用iframe实现页面无刷新上传文件(1/2)

    利用form表单的target属性和iframe 一、上传文件的一个php教程方法。 该方法接受一个$file参数,该参数为从客户端获取的$_files变量,返回重新命名后的文件名,如果上传失...2016-11-25
  • 一个奇葩的最短的 IE 版本判断JS脚本

    使用 conditional comment 来判断 IE 的版本。嗯,是早早有人提出,但没有认真看代码。昨天刚好在看 CSS3 PIE 的时候看到,觉得是不是不靠谱。今天看到 Paul Irish 也提起,那么,推荐一下吧。这是作者博客上写的:复制代码 代码...2014-05-31
  • php批量替换内容或指定目录下所有文件内容

    要替换字符串中的内容我们只要利用php相关函数,如strstr,str_replace,正则表达式了,那么我们要替换目录所有文件的内容就需要先遍历目录再打开文件再利用上面讲的函数替...2016-11-25
  • PHP文件上传一些小收获

    又码了一个周末的代码,这次在做一些关于文件上传的东西。(PHP UPLOAD)小有收获项目是一个BT种子列表,用户有权限上传自己的种子,然后配合BT TRACK服务器把种子的信息写出来...2016-11-25
  • c# 判断是否为空然后赋值的4种实现方法

    下面小编就为大家分享一篇c# 判断是否为空然后赋值的4种实现方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-06-25
  • AI源文件转photoshop图像变模糊问题解决教程

    今天小编在这里就来给photoshop的这一款软件的使用者们来说下AI源文件转photoshop图像变模糊问题的解决教程,各位想知道具体解决方法的使用者们,那么下面就快来跟着小编...2016-09-14
  • C++万能库头文件在vs中的安装步骤(图文)

    这篇文章主要介绍了C++万能库头文件在vs中的安装步骤(图文),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-23
  • Zend studio文件注释模板设置方法

    步骤:Window -> PHP -> Editor -> Templates,这里可以设置(增、删、改、导入等)管理你的模板。新建文件注释、函数注释、代码块等模板的实例新建模板,分别输入Name、Description、Patterna)文件注释Name: 3cfileDescriptio...2013-10-04
  • C#路径,文件,目录及IO常见操作汇总

    这篇文章主要介绍了C#路径,文件,目录及IO常见操作,较为详细的分析并汇总了C#关于路径,文件,目录及IO常见操作,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
  • php文件上传你必须知道的几点

    本篇文章主要说明的是与php文件上传的相关配置的知识点。PHP文件上传功能配置主要涉及php.ini配置文件中的upload_tmp_dir、upload_max_filesize、post_max_size等选项,下面一一说明。打开php.ini配置文件找到File Upl...2015-10-21
  • ant design中upload组件上传大文件,显示进度条进度的实例

    这篇文章主要介绍了ant design中upload组件上传大文件,显示进度条进度的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-10-29