2

私がそれをしようとすると:

Directory.CreateDirectory([folderPath]);
File.CreateText([folderPath]);

私には例外がありました-

System.UnauthorizedAccessException was unhandled by user code
  HResult=-2147024891
  Message=Access to the path 'C:\[folderPath]' is denied.
  Source=mscorlib
  StackTrace:
       at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
       at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
       at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
       at System.IO.StreamWriter.CreateFile(String path, Boolean append, Boolean checkHost)
       at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize, Boolean checkHost)
       at System.IO.StreamWriter..ctor(String path, Boolean append)
       at System.IO.File.CreateText(String path)
       at Contest.Service.FileSystem.CreateFileAndFolderForAttempt(IAttempt attempt) in c:\Users\Mif\Documents\Visual Studio 2012\Projects\Contest\Contest.Service\FileSystem.cs:line 33
       at Contest.Service.Tests.FileSystemTests.TestCreateFileAndFolderForAttempt() in c:\Users\Mif\Documents\Visual Studio 2012\Projects\Contest\Contest.Service.Tests\UnitTest1.cs:line 32
  InnerException:

ファイル エクスプローラーで次のように表示されます。

属性: [トライステート/不確定チェックボックス] 読み取り専用 (フォルダー内のファイルにのみ適用)

でもDebug.WriteLine(CurrentContestDirectory.Attributes.ToString()); 見せて―― Directory。readonly 属性ではありません。

Google で「C# を使用してディレクトリの読み取り専用属性を削除する」を探しましたが、すべての回答が役に立ちません。私は何をする必要がありますか?

すべてのコードは、Windows 8 のローカル マシンで実行されます。管理者アカウント。readonly 属性を手動でオフにすると、コードの実行後に元に戻ります。

シンプルなコンソールアプリです。

今、手動でフォルダーを作成します。デフォルトでは読み取り専用です。

attrib -r C:\new /s in cmd が機能しない!!!

手動で読み取り専用のチェックを外して [OK] をクリックすると、[プロパティ] をクリックすると読み取り専用に戻ります

4

3 に答える 3

1

このコードで試してください:

 Directory.CreateDirectory(@"c:\newfolder");
 File.CreateText(@"c:\newfolder\textfile.txt");
于 2012-11-13T01:53:40.277 に答える
0

このコードを実行しているアプリケーション自体が Windows で適切な権限を持っていることを確認します。たとえば、アプリケーションを右クリックし、[管理者として実行] をクリックします。

于 2012-11-13T01:53:29.670 に答える