NetworkService权限(多语言)

时间:2010-03-30 13:35:16

标签: vb.net permissions globalization

所以,我正在做一个用VB.NET编写的安装项目,我需要向NetworkService帐户授予某个文件夹的权限。

以下代码非常完美(Windows 7 - en-US):

Dim dInfo As New DirectoryInfo("C:\FolderOrFileToGivePermission")
Dim dSecurity As DirectorySecurity = dInfo.GetAccessControl()
dSecurity.AddAccessRule(New FileSystemAccessRule("NETWORK SERVICE", FileSystemRights.FullControl, AccessControlType.Allow))
dInfo.SetAccessControl(dSecurity)

当我在Windows 7,Vista或XP(所有在PT-BR )上尝试使用相同的代码时,问题就出现了,我发现没有“网络服务”,正确的名称是“ServiçodeRede”。

我需要获得此名称才能授予合适的用户权限。

经过对所有3个操作系统的大量调查,我发现用户的ID是:“S-1-5-20”,他在注册表中的路径是:Computer \ HKEY_USERS \ S-1-5- 20 及其默认文件夹:C:\ Windows \ ServiceProfiles \ NetworkService

但我仍然没有找到实际的“可本地化”名称,我需要它是动态的,因为这个系统将安装在许多不同的国家(不同的机器和文化)。

提前致谢。

2 个答案:

答案 0 :(得分:5)

使用System.Security.Principal.WellKnownSidType枚举:

SecurityIdentifier networkService = new SecurityIdentifier(
    WellKnownSidType.NetworkServiceSid, null);

FileSystemAccessRule rule = new FileSystemAccessRule(
    networkService, FileSystemRights.FullControl, AccessControlType.Allow);

答案 1 :(得分:1)

所以,经过越来越多的研究,我已经改变了对Google和stackoverflow的搜索,我在另一个问题上找到了答案:

The best way to resolve display username by SID?

Dim NetworkServiceName as String = new SecurityIdentifier("S-1-5-20").Translate(typeof(NTAccount)).ToString();
相关问题