我正在使用 c# 制作一个工具,它遍历一个大文件目录并提取某些信息。该目录是按语言(LCID)组织的,所以我想使用多线程来浏览目录——每个语言文件夹一个线程。
我的代码目前在没有多线程的情况下扫描少量文件并提取所需的数据,但在大规模上它会花费太长时间。
我在循环中设置了一个线程来获取 LCID 文件夹,但出现以下错误:“'HBscan' 没有重载与委托 System.threading.threadstart 匹配”。根据我在网上阅读的内容,然后我将我的方法放在一个类中,这样我就可以有参数了,现在没有错误,但是代码没有正确地遍历文件。它正在将文件排除在扫描之外。
我想知道是否有人可以看到我的代码哪里出了问题,导致它无法正常执行?谢谢。
public static void Main(string[] args)
{
//change rootDirectory variable to point to directory which you wish to scan through
string rootDirectory = @"C:\sample";
DirectoryInfo dir = new DirectoryInfo(rootDirectory);
//get the LCIDs from the folders
string[] filePaths = Directory.GetDirectories(rootDirectory);
for (int i = 0; i < filePaths.Length; i++)
{
string LCID = filePaths[i].Split('\\').Last();
Console.WriteLine(LCID);
HBScanner scanner = new HBScanner(new DirectoryInfo(filePaths[i]));
Thread t1 = new Thread(new ThreadStart(scanner.HBscan));
t1.Start();
}
Console.WriteLine("Scanning through files...");
}
public class HBScanner
{
private DirectoryInfo DirectoryToScan { get; set; }
public HBScanner(DirectoryInfo startDir)
{
DirectoryToScan = startDir;
}
public void HBscan()
{
HBscan(DirectoryToScan);
}
public static void HBscan(DirectoryInfo directoryToScan)
{
//create an array of files using FileInfo object
FileInfo[] files;
//get all files for the current directory
files = directoryToScan.GetFiles("*.*");
string asset = "";
string lcid = "";
//iterate through the directory and get file details
foreach (FileInfo file in files)
{
String name = file.Name;
DateTime lastModified = file.LastWriteTime;
String path = file.FullName;
//first check the file name for asset id using regular expression
Regex regEx = new Regex(@"([A-Z][A-Z][0-9]{8,10})\.");
asset = regEx.Match(file.Name).Groups[1].Value.ToString();
//get LCID from the file path using regular expression
Regex LCIDregEx = new Regex(@"sample\\(\d{4,5})");
lcid = LCIDregEx.Match(file.FullName).Groups[1].Value.ToString();
//if it can't find it from filename, it looks into xml
if (file.Extension == ".xml" && asset == "")
{
System.Diagnostics.Debug.WriteLine("File is an .XML");
System.Diagnostics.Debug.WriteLine("file.FullName is: " + file.FullName);
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(path);
//load XML file in
//check for <assetid> element
XmlNode assetIDNode = xmlDoc.GetElementsByTagName("assetid")[0];
//check for <Asset> element
XmlNode AssetIdNodeWithAttribute = xmlDoc.GetElementsByTagName("Asset")[0];
//if there is an <assetid> element
if (assetIDNode != null)
{
asset = assetIDNode.InnerText;
}
else if (AssetIdNodeWithAttribute != null) //if there is an <asset> element, see if it has an AssetID attribute
{
//get the attribute
asset = AssetIdNodeWithAttribute.Attributes["AssetId"].Value;
if (AssetIdNodeWithAttribute.Attributes != null)
{
var attributeTest = AssetIdNodeWithAttribute.Attributes["AssetId"];
if (attributeTest != null)
{
asset = attributeTest.Value;
}
}
}
}
Item newFile = new Item
{
AssetID = asset,
LCID = lcid,
LastModifiedDate = lastModified,
Path = path,
FileName = name
};
Console.WriteLine(newFile);
}
//get sub-folders for the current directory
DirectoryInfo[] dirs = directoryToScan.GetDirectories("*.*");
foreach (DirectoryInfo dir in dirs)
{
HBscan(dir);
}
}
}