日期:2014-05-16  浏览次数:20968 次

CreateDirectory执行没有抛出异常,但是也没有成功创建目录,这是为什么?
我的windows 7安装在d盘,我尝试用C#程序在该盘符下面创建一个名叫abc的目录:

            try
            {
                var dir = new DirectorySecurity();

                DirectoryInfo info = Directory.CreateDirectory(
                    path: @"d:\abc",
                    directorySecurity: dir);
                info.Create();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }

没有进入catch打印异常,但是查看d盘下面也没有文件生成。这到底是什么原因呢?
------解决方案--------------------

            DirectorySecurity securityRules = new DirectorySecurity();
            securityRules.AddAccessRule(new FileSystemAccessRule(@"Domain\account1", FileSystemRights.Read, AccessControlType.Allow));
            securityRules.AddAccessRule(new FileSystemAccessRule(@"Domain\account2", FileSystemRights.FullControl, AccessControlType.Allow));

            DirectoryInfo di = Directory.CreateDirectory(@"C:\destination\NewDirectory", securityRules);