c++ - C++ 检查文件是否存在

标签 c++ file file-exists

我是 C++ 的新手。 在我当前的项目中,我已经包含了

#include <iostream>
#include <Windows.h>
#include <TlHelp32.h>

我只需要在 main() 的最开始快速检查一下,看看我的程序目录中是否存在所需的 dll。 那么对我来说最好的方法是什么?

最佳答案

因此,假设可以简单地检查同一目录中是否存在具有正确名称的文件:

#include <fstream>

...

void check_if_dll_exists()
{
    std::ifstream dllfile(".\\myname.dll", std::ios::binary);
    if (!dllfile)
    {
         ... DLL doesn't exist... 
    }
}

如果您想知道它实际上是一个真正的 DLL(而不是有人打开命令提示符并执行 type NUL: > myname.dll 来创建一个空文件),您可以使用:

HMODULE dll = LoadLibrary(".\\myname.dll");

if (!dll)
{
   ... dll doesn't exist or isn't a real dll.... 
}
else
{
   FreeLibrary(dll);
}

关于c++ - C++ 检查文件是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17005006/

相关文章:

java - 如何创建带有图标和文本的 JList?

c# - 检查文件是否存在异步?

php - Laravel 5.2 中如何检查文件是否存在

c++ - 字符串表编码与 gzip 压缩

c# - 如何检测键盘上特殊键的向下/向上事件

delphi - 将对象列表保存在文本文件中

python - 检查目录是否包含具有给定扩展名的文件

c++ - 计算平均值 : different result for masked image vs ROI

c++ - Lint (CLang) 对 'override' 的特化投诉

java - 程序在Eclipse中运行,导出的可运行jar文件打不开?