Jump to content

C# Windows Active User

Go to solution Solved by lubblig,

You use something called an environment variable

%userprofile% takes you to the current user's folder, then %userprofile%\Desktop takes you to the desktop.

(It's not just C#, try it out in windows explorer smile.png)

 

Here are some more ones that are set by default in Windows that you may find useful:

http://ss64.com/nt/syntax-variables.html

(You can set custom ones too, for example things like the java path can be set)

 

You can also use the following command (for C#):

Environment.GetFolderPath(Environment.SpecialFolder.PLACE);

Where PLACE is where you what folder you want. If you type this and after SpecialFolder enter the dot. The intellisense will show and give you the different options.

 

This can be stored in a string btw.

 

It'll look like this:

 

code.png

Hey guys!

 

How can i get the Active windows account username? For example if i want my program to create a folder on the active user's desktop i need the full path from the C drive to the desktop (e.g @"C:\Users\John\Desktop\")

 

How do i get that "John" username?

 

Thanks in advance!

 

P.s programming language is C#

99 little bugs in the code, 99 little bugs, you take one down, you pass it around... 10335 little bugs in the code :mellow:

 

Intel Core i7 3770, ASUS Maximus IV Gene-Z, DDR3 @1866MHz 16GB (2x8GB), GIGABYTE GTX 1060 G1 Gaming 6GB

Link to comment
https://linustechtips.com/topic/482980-c-windows-active-user/
Share on other sites

Link to post
Share on other sites

You use something called an environment variable

%userprofile% takes you to the current user's folder, then %userprofile%\Desktop takes you to the desktop.

(It's not just C#, try it out in windows explorer :))

 

Here are some more ones that are set by default in Windows that you may find useful:

http://ss64.com/nt/syntax-variables.html

(You can set custom ones too, for example things like the java path can be set)

Aragorn (WS): 250D | 6800k | 840 Pro 512GB | Intel 530 480GB  | Asus X99-M WS | 64GB DDR4 | Corsair HX720i | GTX 1070 | Corsair H115i | Philips BDM4350UC 43" 3840x2160 IPS

Gimli (server):  Node 304 | G4560 | ADATA XPG SX8000 128GB | 2x 5TB WD Red | ASROCK H270M-ITX/AC  | 8GB DDR4 | Seasonic 400FL

 Omega (server):                 Fractal Arc Mini R2 | i3 4130 | 500GB Maxtor | 2TB WD Red : Raid 1 | 3TB Seagate Barracuda | 16GB RAM | Seasonic G-450w
Alpha (WS): 900D | 4770k | GTX 780  | 840 Pro 512GB  | GA-Z87X-OC | Corsair RM 850 | 24GB 2400mhz | Samsung S27B970D 2560x1440

                              ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

Link to comment
https://linustechtips.com/topic/482980-c-windows-active-user/#findComment-6478802
Share on other sites

Link to post
Share on other sites

You use something called an environment variable

%userprofile% takes you to the current user's folder, then %userprofile%\Desktop takes you to the desktop.

(It's not just C#, try it out in windows explorer smile.png)

 

Here are some more ones that are set by default in Windows that you may find useful:

http://ss64.com/nt/syntax-variables.html

(You can set custom ones too, for example things like the java path can be set)

 

You can also use the following command (for C#):

Environment.GetFolderPath(Environment.SpecialFolder.PLACE);

Where PLACE is where you what folder you want. If you type this and after SpecialFolder enter the dot. The intellisense will show and give you the different options.

 

This can be stored in a string btw.

 

It'll look like this:

 

code.png

Spoiler

System:

i5 3570k @ 4.4 GHz, MSI Z77A-G43, Dominator Platinum 1600MHz 16GB (2x8GB), EVGA GTX 980ti 6GB, CM HAF XM, Samsung 850 Pro 256GB + Some WD Red HDD, Corsair RM850 80+ Gold, Asus Xonar Essence STX, Windows 10 Pro 64bit

PCPP:

http://pcpartpicker.com/p/znZqcf

 

Link to comment
https://linustechtips.com/topic/482980-c-windows-active-user/#findComment-6478825
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×