How can I get a list Local Windows Users (Only the Users that appear in the windows Logon Screen)

Just add a reference to System.Management in a Console Application and try this code: using System; using System.Management; using System.Linq; namespace ConsoleApplication5 { class Program { static void Main(string[] args) { ManagementObjectSearcher usersSearcher = new ManagementObjectSearcher(@”SELECT * FROM Win32_UserAccount”); ManagementObjectCollection users = usersSearcher.Get(); var localUsers = users.Cast<ManagementObject>().Where( u => (bool)u[“LocalAccount”] == true && (bool)u[“Disabled”] == … Read more

create local user account

I had a very similar issue change the first line to PrincipalContext context = new PrincipalContext(ContextType.Machine, “127.0.0.1”); see if that fixes your issue. And triple check that the program is running with administrator privileges. The other issue it could be is the server has password complexity requirements and password that is being passed in to … Read more

How to grant full permission to a file created by my application for ALL users?

Note to people using this. When using literal strings for the FileSystemAccessRule, it should be WellKnownSidType.WorldSid instead of “everyone”. The reason is because there are multiple Window languages and Everyone only applies to EN ones, so for Spanish, it might be “Todos” (or something else). using System.Security.AccessControl; using System.Security.Principal; using System.IO; private void GrantAccess(string fullPath) … Read more

Start / Stop a Windows Service from a non-Administrator user account

Below I have put together everything I learned about Starting/Stopping a Windows Service from a non-Admin user account, if anyone needs to know. Primarily, there are two ways in which to Start / Stop a Windows Service. 1. Directly accessing the service through logon Windows user account. 2. Accessing the service through IIS using Network … Read more

Detect if running as Administrator with or without elevated privileges?

Try this out: using Microsoft.Win32; using System; using System.Diagnostics; using System.Runtime.InteropServices; using System.Security.Principal; public static class UacHelper { private const string uacRegistryKey = “Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System”; private const string uacRegistryValue = “EnableLUA”; private static uint STANDARD_RIGHTS_READ = 0x00020000; private static uint TOKEN_QUERY = 0x0008; private static uint TOKEN_READ = (STANDARD_RIGHTS_READ | TOKEN_QUERY); [DllImport(“advapi32.dll”, SetLastError = true)] [return: … Read more