2016-12-05 10 views
1

おはようみんなで発生したC#の型「System.UnauthorizedAccessException」の未処理の例外は、がmscorlib.dll

私は、アプリケーションのセットアップウィザードを作成した

、私はアプリケーションを実行しているとき、私は、TXTファイルからのデータを見ることができますinstaledフォルダ内の、しかし私は、私はタイプ「System.UnauthorizedAccessException」の未処理の例外がmscorlib.dllが 追加情報で発生したエラー

持っているtxtファイル内の編集データをしようとしているとき:「パスへのアクセスをC:\ Program Files(x86)\ Jean-Paul Sartre Variety Theatre \ CustomerStorage.txt 'が拒否されました。

string text = ""; 
StreamWriter sw = new StreamWriter("CustomerStorage.txt"); //THAT LINE GENERATE ERROR 

foreach (var item in Customers) 
{ 
    text = item.Value.CustomerName + "*" + item.Value.CustomerEmail + "*" + (int)item.Value.Customertype + "*" + item.Value.BookDate + "*" + item.Value.CustomerNo + "*" + item.Value.BookedPlayName + "*" + item.Value.PaymentStatus + "*" + item.Value.SeatNoOne + "*" + item.Value.SeatNoTwo + "*" + item.Value.SeatNoThree + "*" + item.Value.SeatNoFour + "*" + item.Value.SeatNoFive + "*" + item.Value.SeatNoSix + "*" + item.Value.PriceToPay + "*" + item.Value.GetPlayNo + "*" + item.Value.TotalNumberOfSeatsCustomer + "*"; 
    if (item.Value.Customertype == CustomerType.GoldMember) 
    { 
     GoldMember gm = (GoldMember)item.Value; 
     //saving gold member with new created date 
     gm.CreatedDate = gm.CreatedDate; 
     text += gm.CreatedDate + "*"; 
    } 
    sw.WriteLine(text); 
} 
sw.Close(); 

私はセットアップウィザードを作成する時に任意のミスを犯し、私は、ファイルシステムフォルダにtxtファイルを追加する時の許可のようないろいろ書いをしなければならない場合、私は知りません。

+3

アプリケーションは管理者権限で実行されますか?既定では、管理者のアクセス許可なしで実行されているアプリケーションは、UACによってProgram Filesフォルダおよびそのサブフォルダに書き込まれません。 CustomerStorageが編集可能なファイルの場合、ローカルアプリケーションデータフォルダ(c:¥Users¥¥AppData¥Local¥) – Pavel

+0

のどこかに保存する方が、アプリケーションに書き込み権限があることを確認しましたか? –

+0

あなたのコードをリファクタリングして 'StreamWriter'オブジェクトを' using() 'コードブロック構造の周りにラップして、オブジェクトのAuto Disposingを処理/利用します。 – MethodMan

答えて

0

アクセス権の付与方法が問題をスローしました。ありがとうございます。

public bool GrantAccess(string fullPath) 
    { 
     DirectoryInfo dInfo = new DirectoryInfo(fullPath); 
     DirectorySecurity dSecurity = dInfo.GetAccessControl(); 
     dSecurity.AddAccessRule(new FileSystemAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), FileSystemRights.FullControl, InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit, PropagationFlags.NoPropagateInherit, AccessControlType.Allow)); 
     dInfo.SetAccessControl(dSecurity); 
     return true; 
    } 
関連する問題