# How can I make my program get a username?

I am making a program that I want to load music from library/music on any computer. To do this I use Music.FromUri(). However, I need the username from the current computer in order for the filepath to work.

Is there any way to get the username and use it in the filepath?

• If you're planning on constructing a path like C:\Users\Foo\Music, don't. That assumes that the Users directory is named like that, it assumes that the user profile lives in that directory, it assumes that Music is not redirected, it assumes that there in fact exists a C: drive, and that that's where Users are at. If you want a particular Known Folder, use the managed flavors of SHGetFolderPath or SHGetKnownFolderPath to properly retrieve it. – Lars Viklund May 13 '12 at 10:26
• and how do i use those? – Eric Nilsson May 13 '12 at 11:00
• Wouldn't it be better to let the user choose where to load the music from manually? Personally I've never put any music in the Library. – David Gouveia May 13 '12 at 11:03
• Well, if i use Music.FromUri it reads in all subfiles from a file so there will be very many files to chose from. Is there any way to make the FromUri class just read a file and not subfiles? – Eric Nilsson May 13 '12 at 11:12
• What's a subfile? – David Gouveia May 13 '12 at 11:13

You can get the current username with:

Environment.UsernName


But if you were going to use it to construct a path to the Music folder, there's an easier way:

Environment.GetFolderPath(Environment.SpecialFolder.MyMusic)


Here's a list of all the folders you can get this way.

You used the XNA tag, so this should give you what you want: Environment.UserName. Here's the doc.