3

読み取り専用ディレクトリに書き込む(たとえば、ファイルをコピーする)ことができることがわかりました。つまり、...Attributes = FileAttributes.ReadOnly.Iのあるディレクトリはその名前を変更することもできます。私が見つけた唯一のことは、それを削除することです。それが本当にReadOnly防ぐ唯一のことですか?

編集:

コードは次のとおりです:(ディレクトリは空です。)

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

例外をスローしAccess to the path 'c:\... is denied.ます。

しかし、それに変更ReadOnlyした後Normalは正常に動作します。

では、何ReadOnly防止され、何が防止されないのでしょうか。(もちろん、プログラムで。Windowsエクスプローラーではありません。)

編集2:

私は言ってドキュメントへのリンクの答え受けているReadOnlyディレクトリに光栄されていないが、それはおそらく.Netだということは、障害者の削除を担当します。そこで、「 C#/。Netを使用する場合、読み取り専用はディレクトリにどのように影響しますか?」という質問を言い換えます。

4

3 に答える 3

1

そうではありません。十分な数のレイヤーをはがすと、ディレクトリの属性を変更するために使用される関数は次のSetFileAttributesとおりです。

ファイルまたはディレクトリの属性を設定します。

そして注意:

FILE_ATTRIBUTE_READONLY 1(0x1)読み取り専用のファイル。アプリケーションはファイルを読み取ることはできますが、ファイルへの書き込みやファイルの削除はできません。この属性はディレクトリでは適用されません。詳細については、「Windows Server 2003、Windows XP、またはWindows Vistaでは、フォルダの読み取り専用属性またはシステム属性を表示または変更することはできません」を参照してください。

(私の強調)

于 2012-11-02T11:19:25.637 に答える
1

Damien_The_Unbelieverが言及しているように、FILE_ATTRIBUTE_READONLYのWin32 APIを見ると、次のように言及されています。

この属性はディレクトリでは適用されません。

参照: http: //go.microsoft.com/fwlink/p/?linkid = 125896

したがって、win32またはExplorerを使用してそのようなディレクトリを簡単に削除できるようです。ただし、.NETは、ディレクトリを削除する前に、ディレクトリのフラグをチェックしているようです。これは、たとえばDirectory.DeleteでDotPeekまたはReflectorを使用して確認できます。これが「アクセス拒否」エラーの原因です。

編集:

これをもう少し詳しく調べたところ、アクセス拒否エラーをスローしているのは.NETではないようです。次のテストコードを検討してください。

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();
      }
   }
}

私のマシン(win 7)でこれを実行すると、次の出力が得られます。

    Directory.Delete:パス'C:\ ... \ Local \ Temp\a4udkkax.jcy'へのアクセスが拒否されました。
    RemoveDirectory Win32エラー:5
    Directory.Delete:通常のディレクトリを削除しました
    RemoveDirectory:通常のディレクトリを削除しました

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.

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.

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.

于 2012-11-02T11:27:01.523 に答える
1

ファイルシステムのディレクトリエントリの読み取り専用属性の有用性は限られています。ほとんどのユーザーは、ディレクトリ内のすべてのファイルも読み取り専用になることを期待します。ただし、これはNTFSの動作方法ではなく、属性はファイルシステムオブジェクト自体にのみ適用され、セキュリティ属性のように「継承」されることはありません。

ExplorerがUIでの属性の動作をどのように変更したかに注意してください。これをオンにすると、ユーザーが期待するとおりに実行され、ディレクトリ自体に属性を設定する代わりに、すべてのファイルが読み取り専用になります。

しかし、はい、それ機能します、それはディレクトリオブジェクト自体への変更を防ぎます。したがって、コードでオンにすると、ディレクトリの削除が妨げられます。ファイルの場合と同じように。

于 2012-11-02T12:37:47.940 に答える