我有以下代碼:繼續如果文件不存在
public static void readtext(string pathtoText)
{
if (File.Exists(pathtoText))
{
string[] lines = System.IO.File.ReadAllLines(pathtoText);
// Display the file contents by using a foreach loop.
foreach (string line in lines)
{
clearPath(line);
}
}
else
{
Console.WriteLine("{0} doesn't exist, or isn't a valid text file", pathtoText);
}
}
public static void clearPath(string path)
{
if(Directory.Exists(path))
{
int directoryCount = Directory.GetDirectories(path).Length;
if(directoryCount > 0)
{
Console.WriteLine("{0} has Subdirectories to Remove", path);
DirectoryInfo di = new DirectoryInfo(path);
foreach (DirectoryInfo dir in di.GetDirectories())
{
dir.Delete(true);
}
}
else
{
Console.WriteLine("{0} has no directories to remove", path);
}
int fileCount = Directory.GetFiles(path).Length;
if (fileCount > 0)
{
Console.WriteLine("{0} has files to Remove", path);
System.IO.DirectoryInfo di = new DirectoryInfo(path);
foreach (FileInfo file in di.GetFiles())
{
try
{
file.Delete();
}
catch(System.IO.IOException)
{
Console.WriteLine("Please Close the following File {0}", file.Name);
}
}
}
}
else
{
Console.WriteLine("Path Doesn't Exist {0}", path);
}
}
我的功能從文本文件中讀取目錄,並將它傳遞給它的ClearPath檢查,如果目錄存在,如果是這樣,清除它。我的問題是,如果一個目錄不存在,它會停止程序(它不會移動到下一個目錄來檢查它是否存在並且乾淨,每個循環剛剛停止)。
即使特定目錄不存在,我如何才能將它下載到下一個目錄?
是否缺少參數0在最後Console.WriteLine?你用什麼意思停止了節目_? – Steve
你可以發佈'clearPath'的代碼嗎? – Jaco
@Jaco - 問題出在foreach循環中,clearPath方法發佈。 –