ADTechno
ADTechno

Reputation: 85

c# opening up Picture folder

Today is my first day at learning C#, or any programming language so sorry if I get some terms wrong

I have a way of doing this but not the way I'd like and want to know if there's a better solution. Aim is, on my C# program, when you click a button, it opens up the Picture folder.

I use the following code to do so:

private void picBox_Click(object sender, EventArgs e)
{
    Process.Start("explorer.exe", @"C:\Users\MYUSERNAME\Pictures");
}

The code works however where MYUSERNAME is, that is fixed. What if I was to run the program on another machine account where the username is different. Is there a way to auto detect the name?

Upvotes: 2

Views: 2223

Answers (2)

Mokhtar Ashour
Mokhtar Ashour

Reputation: 600

Environment.GetFolderPath(Environment.SpecialFolder.MyPictures)

Special folders enumeration https://msdn.microsoft.com/en-us/library/system.environment.specialfolder(v=vs.110).aspx

Upvotes: 1

pinkfloydx33
pinkfloydx33

Reputation: 12789

You can use Environment.GetFolderPath and the MyPictures or CommonPictures member of the Environment.SpecialFolder enumeration:

var path = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
Process.Start("explorer.exe", path);

MyPictures will go to the folder you are looking for (current user's pictures). But if you wanted to go to the common/shared pictures folder, CommonPictures would work.

Upvotes: 4

Related Questions