拒绝访问错误:使用管理员权限将数据写入文件 [英] Access Denied Error: Writing data to a file with admin access

查看:90
本文介绍了拒绝访问错误:使用管理员权限将数据写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法将任何数据保存到所需的文件中.运行程序时,遇到错误:

I could not save any data into my desired file. When I run the program, I encounter an error:

Access to the path ''C:\Windows\system32\RMSS.ini'' is denied.


这是我的代码:


Here is my code:

String RMSSfile = @"c:\windows\system32\RMSS.ini";
FileStream FS = new FileStream(RMSSfile, FileMode.Create, FileAccess.Write);
StreamWriter sw = new StreamWriter(FS);  
sw.WriteLine(tbFiledata.Text.Trim());
sw.Close();



非常感谢您的帮助.



Any help is greatly appreciated.

推荐答案

将数据写入具有管理员访问权限的文件
您如何/在何处使用管理员权限访问文件的?上面的代码没有显示出来.

错误显然表示该文件的安全配置未正确设置,因此您可以通过代码对其进行访问.此外,别忘了它是您尝试访问的Windows \ System32文件夹.

提供适当的许可.您可能需要模仿.如果是Win7,UAC可能会很高,需要降低.
Writing data to a file with admin access
How/Where did you used the admin privilege to access the file? Above code does not show that.

Error clearly means that the security configuration is not properly set for the file such that you can access it via code. Further, don''t forget it''s a Windows\System32 folder that you are trying to access.

Provide proper permission. You might need to impersonate. If it''s Win7, UAC might be high and needs to be lowered.


如果您使用的是Windows 7,请检查您是否以管理员身份运行程序.

访问被拒绝的消息 [ ^ ]
If you are using windows 7 check that you run your program as administrator.

Access denied message[^]


太糟糕了,这是保存文件的错误位置.您永远不应将其保存在那里.同样,使用任何硬编码的路径名也无济于事.

使用System.Environment.GetFolderPath方法找出合法的目录来保存文件.另请参见System.Environment.SpecialFolder枚举.一个典型的选择是使用System.Environment.SpecialFolder.LocalApplicationDataCommonApplicationData.

—SA
Too bad, this is wrong place to save files. You never should save it there. Also, using any hard-coded path names will lead you nowhere.

Use System.Environment.GetFolderPath method to find out legitimate directories for saving file. See also System.Environment.SpecialFolder enumeration. A typical option is to use System.Environment.SpecialFolder.LocalApplicationData or CommonApplicationData.

—SA


这篇关于拒绝访问错误:使用管理员权限将数据写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆