How do I access special directories in Windows? - c#

What is the C# syntax to retrieve the user's desktop, documents folder and other system folders on Windows?

You can use Environment.GetFolderPath with the Environment.SpecialFolder enumeration. For example:
string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

Use:
string folder = Environment.GetFolderPath(Environment.SpecialFolder.*);
Where * is one of the enum values.

System.Environment.SpecialFolder.MyComputer
etc.

Use Environment.GetFolderPath Method

Related

How to set a specific location to a string in C#

How do I go about getting the info from the computers name and setting that location in a string.
Example*
string contents = File.ReadAllText(#"C:\Users\" + Settings.Default.User + "\\Documents\\My vs\\juice.txt");
Problem is, I cannot use the Setting.Default.User location because that name constantly changes.
I need that section to be some kind of variable that means the computers name....
I don't wanna just hard code in my computers name either because If I put this on my other computers then obviously the name would change
you are looking for Environment class:
you can use Environment.MachineName Property
Anyway if you need a special folder path you can use: Environment.GetFolderPath
there you can find all the special folder available Environment.SpecialFolder Enumeration
You can use the Environment.GetFolderPath method, e.g.:
var folder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
var path = System.IO.Path.Combine(folder, "My vs\\juice.txt");
string contents = File.ReadAllText(path);
You are looking for... Environment.MachineName
You want to use Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) as documented here http://msdn.microsoft.com/en-us/library/system.environment.specialfolder(v=vs.110).aspx

Directory Configuration in C#

I would like to read from a htm file, that is located to the following directory:
C:\Users\**NAME**\AppData\Roaming\Microsoft\Signatures
How can I change the path, so that I can use it from another computer, where the user name is not equal to the one above?
DirectoryInfo directoryInfo = new DirectoryInfo(Environment.SpecialFolder.ApplicationData + #"Roaming\Microsoft\Signatures");
Environment.SpecialFolder is an enumeration, you need to call GetFolderPath to get the actual path. Also ApplicationData includes the "Roaming" part, so you don't need that
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + #"\Microsoft\Signatures"
Try this:
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData,Environment.SpecialFolderOption.None) + #"\Microsoft\Signatures"
Check also the possibility of parameter Environment.SpecialFolderOption

Folder path before project

I am trying to get a folder path in my C drive that I did not want to hard code with my program.
I know that I can use string path = System.AppDomain.CurrentDomain.BaseDirectory; However its give me : C:\FACE\Camera\Camera\bin\Debug . Which I want to only have C:\FACE\ as I want to initialise something in the FACE folder but outside of my Camera folder. I do not wish to hardcode the file path. Is it possible to do it? Thanks for the help!
You could use the method Directory.GetParent for this purpose:
Directory.GetParent("here you will pass you path");
Update
For your case, you could take that you want as below:
string projectPath = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName;
string solutionPath = Directory.GetParent(projectPath).Parent.FullName;
string basePath = Directory.GetParent(solutionPath).FullName;
The variable basePath contains that you want.
System.AppDomain.CurrentDomain.BaseDirectory always returns the path of executable assembly.You can use DTE if you are using web application

How to find the desktop on any user computer?

How can I find the desktop or my documents on any user computer
(I don't know the computer name)
You could use the Environment.GetFolderPath method which will return you the path to the corresponding special folder passed as enum argument. Example:
var desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
You can do find it easily using Environment.SpecialFolder enum.
Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
The Environment.SpecialFolder enumeration has everything you need.
http://msdn.microsoft.com/en-us/library/system.environment.specialfolder.aspx
http://msdn.microsoft.com/en-us/library/14tx8hby.aspx
Use the Environment.SpecialFolder Enumeration together with Environment.GetFolderPath() method, for example:
var desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
This returns the path of the desktop of the currently logged in user. If you want to retrieve the path of the desktop folder for all users, use this instead:
var desktopPath =
Environment.GetFolderPath(Environment.SpecialFolder.CommonDesktopDirectory);

D:\file\file1\SVN how to get folder path?

D:\Saran\Software\SVN this is mypath. How to get only SVN in c#?
Use
string myFileName = Path.GetFileName( #"D:\Saran\Software\SVN" );
myFileName will now contain the string "SVN".
See the System.IO.Path docs for all the functions, and examples, this class provides.
I did just that about ten minutes ago! If you have TortoiseSVN, create a directory, right-click on Windows Explorer and select "SVN Checkout..."
For url, give file:\d\Saran\Software\SVN. Then you get content of the subversion repository.

Categories

Resources