使用C#将文件/文件夹添加到程序文件x86文件夹 [英] Adding Files/Folders to Program Files x86 Folder with C#

查看:133
本文介绍了使用C#将文件/文件夹添加到程序文件x86文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在32位Program Files文件夹中创建文件目录(文件夹),以存储用户将在程序中创建的数据.但是,我一直在说访问被拒绝时会拒绝访问.无论如何,是否要授予程序访问32位程序文件文件夹并向其添加后续文件夹和文件的权限?这是我的下面的代码,由于没有权限,该代码会产生运行时错误.

I am trying to create a file directory (folder) in the 32 bit Program Files folder to store data that a user will be creating in the program. However, it keeps saying that access is denied when I try to do so. Is there anyway to give the program permission to access the 32 bit program files folder and add subsequent folder and files to it? Here is my code below which is producing a run-time error due to not having permission.

string mainDirectory=Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86)+"\\UKC Trial Reporter Files";
if (!Directory.Exists(mainDirectory))
{
    Directory.CreateDirectory(mainDirectory);
}

推荐答案

将数据存储在 Environment.SpecialFolder.ApplicationData Environment.SpecialFolder.LocalApplicationDataEnvironment.SpecialFolder.CommonApplicationData(如果必须由任何用户访问数据).

Store your data in Environment.SpecialFolder.ApplicationData, Environment.SpecialFolder.LocalApplicationData or Environment.SpecialFolder.CommonApplicationData (if data have to be accessible by any user).

切勿将其存储在%ProgramFiles%中. %ProgramFiles%用于...程序文件:可执行文件,dll,app.config文件.

Never store it in %ProgramFiles%. %ProgramFiles% is intended for... program files: executables, dlls, app.config files.

除了安全方面的原因外,还有一个理由永远不要这样做:卸载时,应用程序的安装程序必须清除其创建的所有文件和文件夹.当在应用程序生存期内更改installdir中的文件列表时,这是不可能的.

In addition of security reasons, there's yet another reason to do this never: when uninstalling, your application's installer must sweep all of files and folders it had created. And this is impossible, when list of files in installdir was changed during application lifetime.

幸运的是,自Windows Vista发布以来,我们便拥有了UAC.

Fortunately, we have UAC since Windows Vista has released.

这篇关于使用C#将文件/文件夹添加到程序文件x86文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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