简体   繁体   English

使用.NET代码从用户组访问Program Files文件夹

[英]Access Program Files folder from users group using .NET code

I searched about this on internet but could not find exact answer. 我在互联网上搜索了这个,但找不到确切的答案。 In my application, I have a functionality which copies the files in folder (say pqr) to the location where user wanted. 在我的应用程序中,我有一个功能,可以将文件夹中的文件(比如pqr)复制到用户想要的位置。 The location of source folder is the location of installed application (say c:\\Program Files\\abc\\pqr). 源文件夹的位置是已安装应用程序的位置(例如c:\\ Program Files \\ abc \\ pqr)。 When user is logged in to the machine with 'administrator rights' , user is able to use this functionality. 当用户使用“管理员权限”登录到计算机时,用户可以使用此功能。 But when the user is logged in to the machine with having 'User rights' (non-admin user), this functionality throws exception that access to c:\\Program Files\\abc\\pqr folder is denied . 但是,当用户使用“用户权限” (非管理员用户)登录到计算机时,此功能会抛出拒绝访问c:\\ Program Files \\ abc \\ pqr文件夹的异常。 I tried to elevate user privileges by using below attributes to copy method: 我试图通过使用以下属性来提升用户权限来复制方法:

[PrincipalPermission(SecurityAction.Demand, Role = @"BUILTIN\Administrators")]
[PermissionSet(SecurityAction.Demand, Name="FullTrust")]

I also tried adding manifest file with below changes: 我还尝试使用以下更改添加清单文件:

<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

But none of the approach worked. 但这些方法都没有奏效。

Then I installed my application on D drive. 然后我在D盘上安装了我的应用程序。 And after that when I tried with the non-admin user the functionality worked as expected. 之后,当我尝试使用非管理员用户时,功能按预期工作。
So when the application is installed in C:\\Program Files , functionality was not working for non-admin user.But after installing the application to another location it worked for non-admin user. 因此,当应用程序安装在C:\\ Program Files中时 ,功能对非管理员用户不起作用。但是在将应用程序安装到另一个位置后,它对非管理员用户起作用。

So my question is, is it possible to give the rights if C:\\Program Files to non-admin user programmatically in the application or we need to have admin users for using this functionality? 所以我的问题是,是否有可能在应用程序中以编程方式向非管理员用户提供C:\\ Program Files权限,或者我们需要管理员用户才能使用此功能?

  1. Changing the manifest is most simple way to elevate privileges. 更改清单是提升权限的最简单方法。 Change it in VS. 在VS中更改它 But it is bad practice to require admin privileges for regular software. 但要求常规软件的管理员权限是不好的做法。

  2. You can make program folder accessible for all users during install. 您可以在安装期间为所有用户提供程序文件夹。 For example it can be made with InnoSetup with Permissions: users-modify in [Files] group. 例如,可以使用InnoSetup和[Files]组中的Permissions: users-modify进行制作。

  3. If the source files are in Program Files and you copy in another location, probably the problem is in wrong file access when opening source files. 如果源文件在Program Files并且您在另一个位置复制,则打开源文件时可能是文件访问错误。 Open for reading only. 仅供阅读。

var startInfo = new ProcessStartInfo("yourApplication.exe") { Verb = "runas" };
Process.Start(startInfo);
Environment.Exit(0);

or 要么

application.exit();

if you are using winforms 如果你使用winforms

but be careful to excute this code one time because if you put it in the load event you will get overflow in memory 但要小心一次执行此代码,因为如果你把它放在load事件中,你会在内存中溢出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM