私は大きな問題を抱えています。ここに問題があります。リストビューにリストできるように、ディレクトリからファイル情報を取得しようとしています。その方法を使用してファイルを再帰的に検索すると:
private void Get_Files(string path)
{
DirectoryInfo di = new DirectoryInfo(path);
FileInfo[] fi = di.GetFiles();
foreach (FileInfo Info in fi)
{
try
{
Files.Add(Info.FullName);
}
catch(Exception ee)
{
MessageBox.Show(ee.Message);
}
}
foreach (DirectoryInfo DInfo in di.GetDirectories())
{
Get_Files(DInfo.FullName);
}
}
パスが 260 文字を超える場合があるため、次のエラーが発生します。パスが長すぎます。260 文字を超えてはなりません。インターネットで検索したところ、解決策がないと言われましたが、解決策を見つけました。自分 。解決策:文字列を作成し、パスの各パスをその文字列に追加するため、パス全体を文字列に保存するときにそのエラーが発生することはありません。パスを分解し、各ピースを取得して文字列に追加すると考えてください。だからここに私が考え出した解決策があります:
List<string> Files = new List<string>();
string completepath = string.Empty;
string current_dire_name = string.Empty;
private void Get_Files(string path)
{
DirectoryInfo di = new DirectoryInfo(path);
FileInfo[] fi = di.GetFiles();
foreach (FileInfo Info in fi)
{
try
{
completepath += "\\" + Info.Name;
Files.Add(completepath);
string remove_file_name = completepath;
remove_file_name = remove_file_name.Replace("\\" + Info.Name, "");
completepath = remove_file_name;
}
catch(Exception ee)
{
if(DialogResult.Yes == MessageBox.Show("Error at the Get_Files Method and Error message :\n\n" + ee.Message + "\n\nQuit Application now ?","",MessageBoxButtons.YesNo,MessageBoxIcon.Question))
{
Environment.Exit(0);
}
}
}
foreach (DirectoryInfo DInfo in di.GetDirectories())
{
string remove_folder_name = completepath;
remove_folder_name = remove_folder_name.Replace("\\" + current_dire_name, "");
completepath = remove_folder_name;
current_dire_name = DInfo.Name;
completepath += "\\" + DInfo.Name;
Get_Files(DInfo.FullName);
}
}
わかりました、その方法は私を救いましたが、間違ったパスを生成しました。つまり、何かが正しくないということです。 .txt 、そのようなもの....私が行った方法、特に再帰的な追加に何か問題があることを知っています。
長いパスの例外を回避できるように、誰かが私と一緒にそれを理解してくれることを願っています。