How to Get a Path to the Desktop for Current User in C#

How to get a path to the desktop for current user in C#?

string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

How to get the local desktop path of the users file when the application is hosted from server?

You are hosting a website, which is using a web server like IIS and the user access the site using a browser like edge, chrome, now the file is in user desktop, so the main question remains,

Does the browser process have the access to the file system of the user

Mostly no, especially not via the call Environment.GetFolderPath(Environment.SpecialFolder.Desktop);, this will be good for running it on hosted sever, where w3p.exe process is accessing the file system with required permissions

For the end user desktop

You need to provide the file dialog box, let user select the file / directory and need to plan to serialize the file to the sever (upload) for doing any processing. You can binary serialize the file using a provider like protobuf, msgpack to achieve the necessary functionality

Code you have provided, is good for for the process where you have direct control, like Console, WPF, which runs on the system under certain permission and thus access the file system for processing

How to create the path to desktop for the target user..in C#.net

Next code will return path to the desktop of current user:

Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);

So, in your case it would be

string desktop = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
string image1 = System.IO.Path.Combine(desktop, "r.bmp");

How can i get a user's path after publishing a .NET project?

You can use Enviornment.GetFolderPath() to get the path to a specific folder for the current user:

var desktopFolder = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

Alternatively, another option would be to display a File Save dialog, and that would let the user choose where to save the PDF file. There is a class for WinForms and WPF.

C# reference to the desktop

Quick google search reveals this one:

string strPath = Environment.GetFolderPath(
System.Environment.SpecialFolder.DesktopDirectory);

EDIT: This will work for Windows, but Mono supports it, too.

How can I get the current user directory?

May be this will be a good solution: taking in account whether this is Vista/Win7 or XP and without using environment variables:

string path = Directory.GetParent(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)).FullName;
if ( Environment.OSVersion.Version.Major >= 6 ) {
path = Directory.GetParent(path).ToString();
}

Though using the environment variable is much more clear.



Related Topics



Leave a reply



Submit