How to set a specific location to a string in C# - 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

Related

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

Copy and paste a file programmaticaly

Question Background:
I need to copy and paste (move) a file from one folder location to another.
Issue:
The File.Copy method of System.IO requires the that both parameters are of known file locations. I only know one file path location - in this case localDevPath. localQAPath is the folder path where I want the copied file to be moved too.
string localDevPath = #"C:\Folder1\testFile.cs";
string localQaPath = #"C:\Folder2\";
File.Copy(localDevPath, localQaPath);
Can anyone tell me how to go about carrying out this 'copy and paste' method I'm trying to implement.
string localDevPath = #"C:\Folder1\testFile.cs";
string localQaPath = #"C:\Folder2\";
FileInfo fi = new FileInfo(localDevPath);
fi.MoveTo(Path.Combine(localQaPath, fi.Name));
Assuming that these are user-provided paths and you can't simply include the filename in the second path, then you need to extract the last path element from localDevPath and then add it to localQaPath. You could probably do that with Path.GetFilename.
I'm guessing the issue here is that the filename is variable, in which case, you could do something like this to extract the filename from the full path of localDevPath:
string localDevPath = #"C:\Folder1\testFile.cs";
string localQaPath = #"C:\Folder2\";
string[] tokens = localDevPath.Split(#"\");
localQaPath += tokens[tokens.Length-1];
File.Copy(localDevPath, localQaPath);
Documentation on File.Copy is on MSDN. There is an overload that accepts a boolean, to allow overwriting if there is a naming conflict.
If what you want to do is move the file from one location to another, the method you are looking for is MoveTo. It is a method of the FileInfo class. There is a very complete example in the MSDN Library here: FileInfo.MoveTo Example

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);

How can i get the path of the current user's "Application Data" folder?

1)how can i find out the Windows Installation drive in which the user is working.? I need this to navigate to the ApplicationData in DocumentsandSettings.
2)Also how can i get the user name too so that i can goto ApplicaitionData.? Eg: "D:\Documents and Settings\user\Application Data".
Look at combining Environment.GetFolderPath and Environment.SpecialFolder to do this.
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
Depending on what you are doing you might also want to look at
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)
If the user is on a domain it will only be stored in their local AppData folder and not synced with their roaming profile.
Have a look at the Environment.SpecialFolders
Environment.SpecialFolder.ApplicationData;
Environment.SpecialFolder.System
that should get you round the username requirement as well.
Have a look at the System.Environment class and its properties and methods, e.g:
string systemDir = System.Environment.SystemDirectory;
string docs = System.Environment.GetFolderPath(
System.Environment.SpecialFolder.MyDocuments));
string systemDrive = System.IO.Path.GetPathRoot(systemDir);
The first one returns "C:\Windows\system32" for example and the second one "C:\Documents and Settings\USERNAME\My Documents".
Try this:
string filePath = Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData);
1)how can i find out the Windows Installation drive in which the user
is working.?
var systemDrive = Environment.ExpandEnvironmentVariables("%systemdrive%");
I need this to navigate to the ApplicationData in
DocumentsandSettings.
You don't really require to fetch the value of either system drive or currently logged in user name to achieve this. There are predefined environment variables %localAppData% and %appData% which give you fully qualified path of these directories as shown in the code below:
var localApplicationData = Environment.ExpandEnvironmentVariables("%localappdata%");
//this gives C:\Users\<userName>\AppData\Local
var roamingApplicationData = Environment.ExpandEnvironmentVariables("%appdata%");
//this gives C:\Users\<userName>\AppData\Roaming
2)Also how can i get the user name too so that i can goto
ApplicaitionData.? Eg: "D:\Documents and Settings\user\Application
Data".
Again, you don't need user name to get the application data path as I've discussed above. Still, for the sake of knowledge you can fetch it from %username% environment variable as shown below:
var currentUserName = Environment.ExpandEnvironmentVariables("%username%");

Categories

Resources