小弟是初学者,下面是学习文件和流的程序,问题:
当运行到Directory.Delete(path)时,报错 "The process cannot access the file 'C:\\My Folder2' because it is being used by another process." 可我没有找到其他程序在使用此文件,是不是某个潜在的进程在使用此文件?请高手赐教。
public static void test()
{
string path = "C:\\My Folder";
try
{
if (!Directory.Exists(path))
{
//---创建目录及其子目录---
Directory.CreateDirectory(path);
Directory.SetCurrentDirectory(path);
Directory.CreateDirectory("Subdir1");
Directory.CreateDirectory("Subdir2");
Directory.CreateDirectory("Subdir3");
}
Directory.SetCurrentDirectory(path);
//---打印目录信息---
Console.WriteLine("当前工作目录:"+Directory.GetCurrentDirectory());
Console.WriteLine("创建时间:"+Directory.GetCreationTime(path));
DirectoryInfo parent = Directory.GetParent(path);
if (parent.Exists)
{
Console.WriteLine("父目录信息:");
Console.WriteLine(" "+parent.FullName);
Console.WriteLine(" "+parent.CreationTime);
Console.WriteLine(" "+parent.CreationTimeUtc);
}
//---打印子目录名称---
string[] subDirs = Directory.GetDirectories(path);
Console.WriteLine("子目录:");
foreach (string subDir in subDirs)
{
Console.WriteLine(" " + subDir);
}
//---删除所有目录---
foreach(string str in Directory.GetDirectories(path))
{
Directory.Delete(str);
}
Directory.Delete(path);
}
catch (System.IO.IOException ex)
{
Console.WriteLine(ex.Message);
}
catch (System.Exception ex)
{
Console.WriteLine(ex.Message);
}
}
关闭你的VS,再打开再运行调试,还有那文件夹是不是你正打开呢...
代码没问题啊,我试了一下,我的可以啊
重启机器.
话说回来,有个小程序可以查看锁定文件的进程.