2012-01-23 4 views
2

指定された場所にディレクトリを作成していますが、StreamWriterでアクセスしようとしていますが、「アクセスが拒否されました。作成したディレクトリにアクセスするとUnauthorizedAccessExceptionが発生する

何が起こっているのかも知りませんか?

私のコードはこれです:System.IO.FileStream.Init(文字列のパスにSystem.IO .__ Error.WinIOError(のInt32のerrorCode、文字列maybeFullPath) で

if (!Directory.Exists(dirPath)) 
     Directory.CreateDirectory(dirPath); 
    var a = HasWritePermissionOnDir(dirPath); <- This is only here to see if the value is true and it is. 
    tw = new StreamWriter(dirPath); 

    public static bool HasWritePermissionOnDir(string path) 
    { 
     var writeAllow = false; 
     var writeDeny = false; 
     var accessControlList = Directory.GetAccessControl(path); 
     if (accessControlList == null) 
      return false; 
     var accessRules = accessControlList.GetAccessRules(true, true, typeof(System.Security.Principal.SecurityIdentifier)); 
     if (accessRules == null) 
      return false; 

     foreach (FileSystemAccessRule rule in accessRules) 
     { 
      if ((FileSystemRights.Write & rule.FileSystemRights) != FileSystemRights.Write) continue; 

      if (rule.AccessControlType == AccessControlType.Allow) 
       writeAllow = true; 
      else if (rule.AccessControlType == AccessControlType.Deny) 
       writeDeny = true; 
     } 

     return writeAllow && !writeDeny; 
    } 

フルスタックトレース、FileModeモード、FileAccessアクセス、Int32権限、Boolean useRights、FileShare共有、Int32 bufferSize、FileOptionsオプション、SECURITY_ATTRIBUTES secAttrs、String msgPath、Boolean bFromProxy、Boolean useLongPath) at System.IO.FileStream..ctor(文字列パス、FileModeモード、FileAccessアクセス、FileShare共有、Int32 bufferSize、FileOp System.IO.StreamWriter.ctor(String path、Boolean append、エンコードエンコード、Int32 bufferSize)の System.IO.StreamWriter.CreateFile(String path、Boolean append)の System.IO.StreamWriterの CにおけるAlertDemoSolution.MySqlUnitTest.TestMethod1で.ctor(文字列のパス) ():\ Tempに\ AlertDemoSolution \ AlertDemoSolution \ AlertDemoSolution \ MySqlUnitTest.cs:ライン23

+0

このコードでは、どのようなことを書いていますか? – SLaks

答えて

5

StreamWriterファイルに書き込みます。
ディレクトリに書き込むことはできません。

おそらく、ディレクトリにファイルを作成する必要があります。

+0

DERP !!!ありがとう、たくさんの男!あなたはちょうど今日私にある適切な睡眠を得ることを思い出させた! :) – user1045602

2

StreamWriterをディレクトリで指定することはできません。そのディレクトリ内のファイルのためにFileStreamをポイントする必要があります。

関連する問題