0

Hi all,

I'm new to WPF in c#, and need to know how I can replace my username with something with the basic equivalent to %userprofile% so that the file will run on other computers.

I've looked at many questions like this, but can't seem to find what I'm looking for.

What I have so far..

Process.Start(@"C:\\Users\Alexander\Desktop\She's here\She's here..lnk");

This works on my computer, but I need it to work in all instances.

I've tried using environment.find and I can't seem to understand it.

Pikoh
  • 7,582
  • 28
  • 53
Alex W
  • 3
  • 6
  • Possible duplicate of [How to get a path to the desktop for current user in C#?](https://stackoverflow.com/questions/634142/how-to-get-a-path-to-the-desktop-for-current-user-in-c) & `Path.Combine()` – Alex K. Jul 19 '17 at 13:58
  • The link you just attached has helped, but I need it to be directed to a folder on my desktop and then to the file in that folder. I don't know how to do this. – Alex W Jul 19 '17 at 14:00
  • Possible duplicate of [c# open file, path starting with %userprofile%](https://stackoverflow.com/questions/9993561/c-sharp-open-file-path-starting-with-userprofile) – Glenn Ferrie Jul 19 '17 at 14:01
  • Actually, I think this question is a better fit: https://stackoverflow.com/questions/9993561/c-sharp-open-file-path-starting-with-userprofile – Glenn Ferrie Jul 19 '17 at 14:01

2 Answers2

1

I believe you are looking for this:

Environment.SpecialFolder.Desktop

The logical Desktop rather than the physical file system location.

Which combined with Environment.GetFolderPath returns:

The path to the specified system special folder

So you should use it like this:

string desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
Process.Start(Path.Combine(desktop, "She's here", "She's here..lnk"));
Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
1

You can use use Environment.SpecialFolderNames.

var userFolder = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
var filePath = Path.Combine(userFolder, @"She's here\She's here..lnk");

Environment.SpecialFolder.DesktopDirectory :
The directory used to physically store file objects on the desktop.

NtFreX
  • 10,379
  • 2
  • 43
  • 63