NetworkService permission (multilingual)

So, I am doing an installation project, all written in VB.NET, and I need to give the NetworkService account permission to a specific folder.

The following code works fine (Windows 7 - ru-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) 

The problem started when I try to use the same code on my Windows 7, Vista or XP ( all in PT-BR ), I found that there is no "NETWORK SERVICE", the correct name is "Serviço de Rede".

I need this name to entitle the correct user.

After much research on all three operating systems, I found that the identifier for the user is "S-1-5-20", his path in the registry is Computer \ HKEY_USERS \ S-1-5-20, and the default folder for him is: C: \ Windows \ ServiceProfiles \ NetworkService

But I still haven't found the actual “localizable” name, and I need it to be dynamic, because this system will be installed in many different countries (different machines and cultures).

Thanks in advance.

+4
source share
2 answers

So, after more and more research, I changed my search to Google and stackoverflow, and I found the answer to another question:

Best way to enable username mapping by SID?

 Dim NetworkServiceName as String = new SecurityIdentifier("S-1-5-20").Translate(typeof(NTAccount)).ToString(); 
+1
source

Use the System.Security.Principal.WellKnownSidType enumeration:

 SecurityIdentifier networkService = new SecurityIdentifier( WellKnownSidType.NetworkServiceSid, null); FileSystemAccessRule rule = new FileSystemAccessRule( networkService, FileSystemRights.FullControl, AccessControlType.Allow); 
+5
source

Source: https://habr.com/ru/post/1305546/


All Articles