writeToFile 读写文件问题

关于 writeToFile 读写文件:当字典中键值对以 Model(例如:studentModel)为值时发现 Dictionary 调用 writeToFile 方法无法生成 plist 文件,经过多次测试发现, 调用 writeToFile 方法时 Dictionary 中不能包含 model 对象,针对这一现象,对 model 数据进行处理转化为 Dictionary ,就可以存储为 plist 文件.当读取 plist 文件时再将 Dictionary 转化为 Model.

下面是以 studentModel 为例实现代码:

studentModel 包含三个属性:name,passWord,sex

 

1 @property (nonatomic , copy)NSString *name;
2 @property (nonatomic , copy)NSString *passWord;
3 @property (nonatomic , copy)NSString *sex;

 

 

 

为studentModel 添加以实例方法,完成将 Model 对象转为  Dictionary

1 - (NSMutableDictionary *)changeModelToDic{
2     NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
3     [dic setObject:self.name forKey:@"name"];
4     [dic setObject:self.passWord forKey:@"passWord"];
5     [dic setObject:self.sex forKey:@"sex"];
6     return dic;
7 }

为studentModel 添加类方法.完成将 Dictionary 转化为 Model 对象

1 + (Student*)changDicToStudent:(NSMutableDictionary *)dic{
2     Student *stu = [[Student alloc]init];
3     stu.name = [dic objectForKey:@"name"];
4     stu.passWord = [dic objectForKey:@"passWord"];
5     stu.sex = [dic objectForKey:@"sex"];
6     return stu;
7 }

 

转载于:https://www.cnblogs.com/Ager/p/4907625.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在C#中,要以管理员权限读写文件,可以使用以下代码片段: ``` using System; using System.Security.Principal; public static bool HasAdminRights() { WindowsIdentity identity = WindowsIdentity.GetCurrent(); WindowsPrincipal principal = new WindowsPrincipal(identity); return principal.IsInRole(WindowsBuiltInRole.Administrator); } public static void WriteToFile(string filePath, string content) { if (HasAdminRights()) { // 以管理员权限直接写入文件 System.IO.File.WriteAllText(filePath, content); } else { // 创建一个以管理员权限运行的进程 System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(); startInfo.FileName = System.Windows.Forms.Application.ExecutablePath; startInfo.Arguments = $"\"{filePath}\" \"{content}\""; startInfo.Verb = "runas"; System.Diagnostics.Process.Start(startInfo); } } public static string ReadFromFile(string filePath) { if (HasAdminRights()) { // 以管理员权限直接读取文件内容 return System.IO.File.ReadAllText(filePath); } else { // 创建一个以管理员权限运行的进程 System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(); startInfo.FileName = System.Windows.Forms.Application.ExecutablePath; startInfo.Arguments = $"\"{filePath}\""; startInfo.Verb = "runas"; System.Diagnostics.Process.Start(startInfo); // 等待进程完成后再读取文件内容 System.Threading.Thread.Sleep(2000); return System.IO.File.Exists(filePath) ? System.IO.File.ReadAllText(filePath) : null; } } ``` 使用上述代码,你可以调用`WriteToFile`方法以管理员权限写入文件,调用`ReadFromFile`方法以管理员权限读取文件内容。如果当前用户已经具有管理员权限,则直接进行文件操作;否则,会弹出UAC提示框,用户需要选择以管理员身份运行程序,然后在程序中进行文件操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值