首页 新闻 搜索 专区 学院

文件操作的基础性问题

0
[已解决问题] 解决于 2010-09-01 11:41

小弟是初学者,下面是学习文件和流的程序,问题:

  当运行到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);
            }
           
        }

Green Billow的主页 Green Billow | 初学一级 | 园豆:60
提问于:2010-08-26 11:20
< >
分享
最佳答案
0

关闭你的VS,再打开再运行调试,还有那文件夹是不是你正打开呢...

Astar | 高人七级 |园豆:40805 | 2010-08-26 11:44
其他回答(2)
0

代码没问题啊,我试了一下,我的可以啊

ForrestWoo | 园豆:550 (小虾三级) | 2010-08-26 11:33
0

重启机器.

话说回来,有个小程序可以查看锁定文件的进程.

Launcher | 园豆:45045 (高人七级) | 2010-08-26 13:06
请问叫什么名字的程序啊?
支持(0) 反对(0) Green Billow | 园豆:60 (初学一级) | 2010-08-26 15:53
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册