C#拒绝访问路径在一个Windows应用程序 [英] C# Access denied to path in a Windows Application

查看:684
本文介绍了C#拒绝访问路径在一个Windows应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经建立了使用C#,在Windows 7中的Windows应用程序。

I've built a Windows Application using c#, on Windows 7.

一切工作正常,所以我创建了一个安装向导项目,然后它建成。有一次,我安装的应用程序,我可以正确打开它,但是当我试图让它崩溃一些行动写入一个文本文件(日志记录),thrwoing我下面的错误消息:

Everything was working fine, so I've created a Setup Wizard project and then built it. Once I install the app, I can open it correctly, but when I try to make some action that writes a text file(with logging purposes) it crashes, thrwoing me the following error message:

UnauthorizedAccessException

UnauthorizedAccessException

访问路径C:\Program文件
(86)\MSProgram\\ \\MSProgram\log.txt'是
拒绝。

Access to the path 'C:\Program Files (x86)\MSProgram\MSProgram\log.txt' is denied.

我手动给该文件夹的完全权利,它工作正常。现在的问题是:

When I manually give that folder full rights, it works fine. Now, the question is the following:

我如何编程给我的应用程序的目录写东西的应用程序的权利?所以,每一个下载它的人不会遇到同样的问题。

How do I programmatically give the app rights for writing things in my app's directory? So every person that downloads it doesn't experience the same problem.

推荐答案

不要。应用程序不应直接将数据写入到他们的安装目录。这样做将在Windows Vista和Windows 7较差的应用程序的工作,因为它不保存数据的正确方法。

Don't. Applications should not write data into their installation directory directly. Doing so will make the application work poorly on Windows Vista and Windows 7, since it's not the proper way of saving data.

您应该使用的 Environment.GetFolderPath ,并写入到一个好的位置,比如用户的应用程序数据文件夹(的Environment.SpecialFolders.ApplicationData )。

You should instead use Environment.GetFolderPath, and write into a good location, such as the user's application data folder (Environment.SpecialFolders.ApplicationData).

这篇关于C#拒绝访问路径在一个Windows应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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