我发现C#代码可以循环遍历我们指定的任何目录,我试图将其转换为C ++ / CLI并使其循环遍历所有系统文件&文件夹。问题是代码让你确定需要扫描的深度级别!我需要的是让它循环所有文件&文件夹无论深度水平......这就是我所做的......有一些例外!
#include "stdafx.h"
using namespace System;
using namespace System::IO;
ref class Scan {
public:
static int MaxPath=260000000;
static void ScanMyDir( String^ SourceDir,int RecursiveLevel) {
if(RecursiveLevel<=MaxPath) {
array <String^> ^fileEntries = Directory::GetFiles(SourceDir);
for each (String^ fileName in fileEntries) {
Console::WriteLine(fileName);
}
}
array<String^> ^SubDirEntries = Directory::GetDirectories(SourceDir);
for each (String^ subdir in SubDirEntries)
if ((File::GetAttributes(subdir) & FileAttributes::ReparsePoint)!= FileAttributes::ReparsePoint)
ScanMyDir(subdir,RecursiveLevel+1);
}
};
int main(array<System::String ^> ^args) {
Scan::ScanMyDir("c://",1);
Console::Read();
return 0;
}
经过一些修改(仍有一些错误):
using namespace System;
using namespace System::IO;
static void ScanMyDir( String^ SourceDir) {
array <String^> ^fileEntries = Directory::GetFiles(SourceDir);
for each (String^ fileName in fileEntries) {
Console::WriteLine(fileName);
}
array<String^> ^SubDirEntries = Directory::GetDirectories(SourceDir);
for each (String^ subdir in SubDirEntries)
if ((File::GetAttributes(subdir) & FileAttributes::ReparsePoint)!= FileAttributes::ReparsePoint)
ScanMyDir(subdir);
}
int main(array<System::String ^> ^args) {
try {
ScanMyDir("C://Windows");
}
catch {
Console::WriteLine("Some Denied files or Folders");
}
return 0;
}
这些是错误:
错误1错误C2059:语法错误:'{'c:\ Users \ Ahmed \ documents \ visual studio 2010 \ Projects \ eeee \ eeee \ eeee.cpp 31
错误6错误C2059:语法错误:'}'c:\ Users \ Ahmed \ documents \ visual studio 2010 \ Projects \ eeee \ eeee \ eeee.cpp 37
错误8错误C2059:语法错误:'}'c:\ Users \ Ahmed \ documents \ visual studio 2010 \ Projects \ eeee \ eeee \ eeee.cpp 37
错误5错误C2059:语法错误:'return'c:\ Users \ Ahmed \ documents \ visual studio 2010 \ Projects \ eeee \ eeee \ eeee.cpp 36
错误4错误C2061:语法错误:标识符'WriteLine'c:\ Users \ Ahmed \ documents \ visual studio 2010 \ Projects \ eeee \ eeee \ eeee.cpp 32
错误7错误C2143:语法错误:缺少';'在'}'c:\ Users \ Ahmed \ documents \ visual studio 2010 \ Projects \ eeee \ eeee \ eeee.cpp 37
之前错误2错误C2309:catch处理程序期望带括号的异常声明c:\ Users \ Ahmed \ documents \ visual studio 2010 \ Projects \ eeee \ eeee \ eeee.cpp 31
错误3错误C2319:'catch'后面必须跟一个复合语句。缺少'{'c:\ Users \ Ahmed \ documents \ visual studio 2010 \ Projects \ eeee \ eeee \ eeee.cpp 32
答案 0 :(得分:2)
如果删除该代码的递归级别部分,则应该拥有所需的内容。你提到了3个例外,但我没有看到任何澄清它们的内容。
static void ScanMyDir( String^ SourceDir)
{
array <String^> ^fileEntries = Directory::GetFiles(SourceDir);
for each (String^ fileName in fileEntries)
{
Console::WriteLine(fileName);
}
array<String^> ^SubDirEntries = Directory::GetDirectories(SourceDir);
for each (String^ subdir in SubDirEntries)
if ((File::GetAttributes(subdir) & FileAttributes::ReparsePoint)!= FileAttributes::ReparsePoint)
ScanMyDir(subdir);
}
};
答案 1 :(得分:0)
为了所有圣洁的爱,请不要使用递归。 我回答this question for C# here。转换到C ++ / CLI很简单。如果您不想这样做,请将其编译为自己的程序集(并可选择将其组合到您的程序集中)。