本文介绍了如何只读使用C#时,会影响目录/。NET?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现我可以写(比方说,一个文件复制到)一个只读的目录。也就是说,与 ...目录属性= FileAttributes.ReadOnly 。我甚至可以更改其名称。我发现的唯一的事情是不能做的是将其删除。那是真正的唯一的事情,只读 prevents?

I've found that I can write (say, copy a file into) a Read-Only directory. That is, a Directory with ...Attributes = FileAttributes.ReadOnly.I can even change its name. The only thing I've found that can't be done is to delete it. Is that really the only thing that ReadOnly prevents?

编辑:

这里的code:(该目录为空。)

Here's the code: (The Directory is empty.)

(new DirectoryInfo(path)).Attributes = FileAttributes.ReadOnly;
Directory.Delete(path);

它抛出一个访问路径C:\ ...被拒绝除了

但改变只读正常之后,它工作正常。

But after changing ReadOnly to Normal it works fine.

那么,什么的确实的一个只读 prevent,又是什么不是prevent? (编程,当然的的:。通过Windows资源管理器)

So what does a ReadOnly prevent, and what doesn't it prevent? (Programmatically, of course. Not: through Windows Explorer.)

编辑2:

我收到的答案链接到的文件说,只读未兑现的目录,它可能是这是负责删除的失败。因此,我将改写这个问题:?如何只读影响一个目录的使用C#/时,网络

I have received answers linking to the documentation saying that ReadOnly is not honored on directories, and that it's probably .Net that's responsible for the delete's failure. So I'll rephrase the question: "How does Read-Only affect a Directory when using C#/.Net ?".

推荐答案

由于Damien_The_Unbeliever提到,如果我们看一下对于FILE_ATTRIBUTE_READONLY Win32 API的它提到:

As Damien_The_Unbeliever mentions, if we look at the Win32 API for FILE_ATTRIBUTE_READONLY it mentions:

这个属性不兑现的目录。

另请参阅: http://go.microsoft.com/fwlink/p/ ?的linkID = 125896

所以,看来确实是,你可以简单地删除用win32或浏览器这样的目录。 .NET,然而,似乎在删除之前检查上的目录标志。您可以通过使用DotPeek或反思,在Directory.Delete例如看到这一点。这是什么导致你的拒绝访问错误。

So it seems indeed that you can simply delete such directories using win32 or Explorer. .NET, however, seems to check flags on directories before deleting them. You can see this by using DotPeek or Reflector, on Directory.Delete for example. This is what's causing your "access denied" error.

编辑:

我看着这一点细节,现在看来似乎不是.NET被抛出拒绝访问错误。请看下面的测试code:

I looked into this in a bit more detail, and it seems like it is not .NET which is throwing the access denied error. Consider the following test code:

using System;
using System.IO;
using System.Runtime.InteropServices;

namespace ReadOnlyDirTest
{
   class Program
   {
      [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true, BestFitMapping = false)]
      extern static bool RemoveDirectory(string path);

      static String CreateTempDir()
      {
         String tempDir;
         do
         {
            tempDir = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
         } while (Directory.Exists(tempDir));

         Directory.CreateDirectory(tempDir);
         return tempDir;
      }

      static void Main(string[] args)
      {
         var tempDir = CreateTempDir();

         // Set readonly.
         new DirectoryInfo(tempDir).Attributes |= FileAttributes.ReadOnly;

         try
         {
            Directory.Delete(tempDir);
         }
         catch (Exception e)
         {
            Console.WriteLine("Directory.Delete: " + e.Message);
         }

         if (!Directory.Exists(tempDir))
            Console.WriteLine("Directory.Delete deleted directory");

         try
         {
            if (!RemoveDirectory(tempDir))
               Console.WriteLine("RemoveDirectory Win32 error: " + Marshal.GetLastWin32Error().ToString());
         }
         catch (Exception e)
         {
            Console.WriteLine("RemoveDirectory: " + e.Message);
         }

         if (!Directory.Exists(tempDir))
            Console.WriteLine("RemoveDirectory deleted directory");

         // Try again without readonly, for both.
         tempDir = CreateTempDir();
         Directory.Delete(tempDir);
         Console.WriteLine("Directory.Delete: removed normal directory");

         tempDir = CreateTempDir();
         if (!RemoveDirectory(tempDir))
            Console.WriteLine("RemoveDirectory: could not remove directory, error is " + Marshal.GetLastWin32Error().ToString());
         else
            Console.WriteLine("RemoveDirectory: removed normal directory");

         Console.ReadLine();
      }
   }
}

在我的机器上运行这个(胜利7)我得到以下的输出:

Running this on my machine (win 7) I get the following output:


    Directory.Delete: Access to the path 'C:\...\Local\Temp\a4udkkax.jcy' is denied.
    RemoveDirectory Win32 error: 5
    Directory.Delete: removed normal directory
    RemoveDirectory: removed normal directory

我们看到,我们得到的错误code 5,其中,根据的,是一个拒绝访问错误

We see we get error code 5, which, according to http://msdn.microsoft.com/en-gb/library/windows/desktop/ms681382(v=vs.85).aspx, is an Access Denied error.

我就只能假定浏览器不设置只读属性删除目录,这当然是很容易做到了。该命令命令rmdir 还删除标记为只读的目录中。

I can then only assume that Explorer unsets the readonly attribute before deleting a directory, which is of course easily done. The command rmdir also removes a directory marked as readonly.

由于文档建议只读标志应该是没有兑现上的目录(即使它似乎是在Win 7),我不会依赖此行为。换句话说,我不会依赖只读preventing什么。

As the documentation suggests the readonly flag should is not honoured on directories (even though it seems to be in Win 7), I would not rely on this behaviour. In other words I would not rely on readonly preventing anything.

这篇关于如何只读使用C#时,会影响目录/。NET?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 08:35