嗯,是Win7引起此问题:UnauthorizedAccessException吗? [英] Umm is Win7 causing this problem: UnauthorizedAccessException?

查看:71
本文介绍了嗯,是Win7引起此问题:UnauthorizedAccessException吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我要回到一个被搁置的项目(现在紧急了吗?).问题在于程序使用C:驱动器根目录中的文件记录数据.
下面的代码用于查看文件是否存在(如果未创建的话).现在,我不得不更改PC和操作系统,因为这是写在死机上的XP机器
现在可以在Win7 Pro上运行以下几点的代码错误

Hi All,
I am getting back to doing a project that was put on hold (now it''s urgent?). The issues is the program uses a file in the root of the C: drive for logging data.
The code below was used to see if the file is there if not create it. Now I have had to change PC''s and Operating Systems as the XP machine this was written on died
Now running on Win7 Pro the code errors at the below points

IniFile ini = new IniFile(INIPath + "\\Widget_emmulation.ini");
           MessageBox.Show("Hello");

           FileName = "C:\\Vapour.txt";

           if (!File.Exists(FileName))
           {
               StreamWriter sw = new StreamWriter(FileName);
        sw.WriteLine("Created By " + txtAdminName.Text + "  " + DateTime.Now + "\n");
               sw.WriteLine();
               sw.Close();

           }
           else
           {
               StreamWriter sw = File.AppendText(FileName);
          sw.WriteLine("Open By " + txtAdminName.Text + " at " + DateTime.Now + "\n");
               sw.WriteLine();
               sw.Close();

           }

这是由于代码或安全设置造成的. (如果您必须在计算机上拥有管理员权限才能保存/更改此文件,我会看到问题!)为什么会触发UnauthorizedAccessException .....
格伦
(嗯,尝试...赶快来吧!)

进一步调查发现,这是行
StramWriter sw =新的StreamWriter(FileName);因此,如果必须创建文件,它似乎是程序.

Is this due to the code or a security setting. (I can see issues if you have to Admin rights on the machine to save/alter this file!) Why is it firing an UnauthorizedAccessException.....
Glenn
(Hmmm try...catch here we come!)

Further investigation reveals that it is the line
StramWriter sw = new StreamWriter(FileName); so it appears to be the program if it has to creates the file. It does this in both the version run in debug mode and the compiled (.exe) version?

推荐答案

是-否.

Win7(及其之前的Vista)不允许您在没有管理员权限的情况下将任何文件写入启动驱动器的根目录,这是针对启动系统病毒的一种安全措施-XP和更早版本允许这样做.

因此,某种程度上,是的,这是Win7负责的,但是实际上,尝试在第一时间做到这一点对您的应用程序部分来说是错误的设计! :laugh:

我建议您将数据存储在您始终具有权限的地方:请在此处我应该在哪里存储我的数据? [ ^ ]
Yes - and no.

Win7 (and Vista before it) will not let you write any file to the root directory of your boot drive without Admin permissions, as a security measure against boot system viruses - XP and earlier did allow it.

So in a way, yes, it is Win7 that is responsible, but in practice it is bad design on your apps part to try and do that in teh first place! :laugh:

I would suggest that you store data in places where you will always have permission: have look here Where should I store my data?[^]


这篇关于嗯,是Win7引起此问题:UnauthorizedAccessException吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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