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

 更新时间:2014年10月28日 15:55:23   投稿:shichen2014  
这篇文章主要介绍了VC判断一个文件为目录的方法,在Windows应用程序设计中非常具有实用价值,需要的朋友可以参考下

本文实例讲述了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程序设计有所帮助。

相关文章

  • OpenCV相机标定的全过程记录

    OpenCV相机标定的全过程记录

    这篇文章主要给大家介绍了关于OpenCV相机标定的相关资料,文中通过实例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2022-03-03
  • C语言中字符串与各数值类型之间的转换方法

    C语言中字符串与各数值类型之间的转换方法

    这篇文章主要介绍了C语言中字符串与各数值类型之间的转换方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-03-03
  • C语言单链表的实现

    C语言单链表的实现

    单链表是一种链式存取的数据结构,用一组地址任意的存储单元存放线性表中的数据元素。这篇文章主要介绍了C语言单链表的实现 的相关资料,需要的朋友可以参考下
    2016-04-04
  • c语言实现可自定义的游戏地图

    c语言实现可自定义的游戏地图

    这篇文章主要为大家详细介绍了c语言实现可自定义的游戏地图,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-01-01
  • 汇编语言常见错误信息中文注解

    汇编语言常见错误信息中文注解

    这篇文章主要介绍了汇编语言常见错误信息中文注解,本文收集大部分汇编中常见错误信息及对应的中文注解,需要的朋友可以参考下
    2014-09-09
  • Qt qml实现动态轮播图效果

    Qt qml实现动态轮播图效果

    这篇文章主要为大家详细介绍了Qt和qml实现动态轮播图效果的相关知识,文中的示例代码讲解详细,具有一定的借鉴价值,有需要的小伙伴可以参考一下
    2024-12-12
  • C++版本简易Flappy bird

    C++版本简易Flappy bird

    这篇文章主要介绍了C++版本简易Flappy bird的相关资料,需要的朋友可以参考下
    2015-03-03
  • c++如何使用openssl接口来生成随机数

    c++如何使用openssl接口来生成随机数

    OpenSSL是一个强大的加密库,不仅支持加密解密,还能生成随机数,设置过程包括下载资源文件、配置项目及修改属性页等步骤,确保库文件正确包含,在Visual Studio中正确配置后,可使用RAND_bytes函数生成随机数,此过程需要注意文件路径和附加目录的设置
    2024-10-10
  • VC List Control控件如何删除选中的记录实例详解

    VC List Control控件如何删除选中的记录实例详解

    这篇文章主要介绍了VC List Control控件如何删除选中的记录实例详解的相关资料,需要的朋友可以参考下
    2017-06-06
  • 使用C语言中的time函数获取系统时间

    使用C语言中的time函数获取系统时间

    在C语言中可以使用time函数来获取系统时间,以下对time函数进行了介绍,需要的朋友可以过来参考下
    2013-07-07

最新评论