从程序文件访问时文件访问被拒绝 [英] File access denied while accessing from program files

查看:35
本文介绍了从程序文件访问时文件访问被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在 VS 2010 和 C# 中开发的小型 Winform 应用程序.并且还创建了设置.

I have a small Winform aplication developed in VS 2010 and C#. And created setup also.

我已将应用程序配置文件放在应用程序文件夹路径中以保存用户凭据.安装后应用程序无法访问配置文件,因为我给了程序安装默认路径如下:

I have put application config file in the application folder path for saving user credentials. After the installation application can't access the config file because I given program installation default path as follows:

[ProgramFilesFolder][制造商][ProductName]

[ProgramFilesFolder][Manufacturer][ProductName]

所以我需要将我的用户凭据永久保存在其他地方.我需要读取和写入权限.我还需要记录应用程序异常.

So I need to keep my user credential somewhere else permanently. I need both read and write permissions. Also I need to log the application exceptions.

推荐答案

应该保存用户数据的位置位于以下 Environment.SpecialFolder 位置

The place you are supposed to save user data is in one of the following Environment.SpecialFolder locations

  • ApplicationData - 用作当前漫游用户特定于应用程序数据的公共存储库的目录.漫游用户在网络上的多台计算机上工作.漫游用户的配置文件保存在网络上的服务器上,并在用户登录时加载到系统中.
  • LocalApplicationData - 用作当前非漫游用户使用的特定于应用程序数据的公共存储库的目录.
  • CommonApplicationData - 用作所有用户使用的特定于应用程序数据的公共存储库的目录.
  • ApplicationData - The directory that serves as a common repository for application-specific data for the current roaming user. A roaming user works on more than one computer on a network. A roaming user's profile is kept on a server on the network and is loaded onto a system when the user logs on.
  • LocalApplicationData - The directory that serves as a common repository for application-specific data that is used by the current, non-roaming user.
  • CommonApplicationData - The directory that serves as a common repository for application-specific data that is used by all users.

您可以使用 Enviorment.GetFolderPath

var savePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)),
                            "MyAppName");
//This should return the path  %UserProfile%\Roaming\MyAppName\

对于程序日志,我将使用 CommonApplicationData,因此来自多个用户的日志都收集在一个位置.

For the programs logs I would use CommonApplicationData so the logs from several users are all collected in one location.

这篇关于从程序文件访问时文件访问被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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