大家早上好,
我已经为应用程序创建了设置向导,当我运行应用程序时,我可以看到来自txt文件的数据,这些文件位于instaled文件夹中,但是当我在txt文件中尝试编辑数据时,我遇到了错误
mscorlib.dll中发生未处理的“System.UnauthorizedAccessException”类型异常 附加信息:访问路径'C:\ Program Files(x86)\ Jean-Paul Sartre Variety Theater \ 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文件添加到文件系统文件夹时应该做出类似的许可。
答案 0 :(得分: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;
}