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
Related
I am trying to get the full path a file by its name only.
I have tried to use :
string fullPath = Path.GetFullPath("excelTest");
but it returns me an incorrect path (something with my project path).
I have read somewhere here a comment which says to do the following:
var dir = Environment.SpecialFolder.ProgramFilesX86;
var path = Path.Combine(dir.ToString(), "excelTest.csv");
but I do not know where the file is saved , therefore I do not know its environment.
can someone help me how to get the full path of a file only by its name?
The first snippet (with Path.GetFullPath) does exactly what you want. It returns something with your project path because the program EXE file is located in the project\Bin\Debug path, which is therefore the "current directory".
If you want to search for a file on a drive, you can use Directory.GetFiles, which will recursively search for a file in a directory given a name pattern.
This returns all xml-files recursively :
var allFiles = Directory.GetFiles(path, "*.xml", SearchOption.AllDirectories);
http://msdn.microsoft.com/en-us/library/ms143316%28v=vs.100%29.aspx
http://msdn.microsoft.com/en-us/library/ms143448.aspx#Y252
https://stackoverflow.com/a/9830162/2196124
I guess you're trying to find file (like in windows search), right ?
I'd look into this question - you will find all files that has that string in their filename, and from there you can return full filepath.
var fileList = new DirectoryInfo(#"c:\").GetFiles("*excelTest*", SearchOption.AllDirectories);
And then just use foreach to do you manipulations, e.g.
foreach(string file in fileList)
{
// MessageBox.Show(file);
}
What you're looking for is Directory.GetFiles(), you can read up on it here. The gist of it is, you'll pass in the file path and the file name, and you'll get a string array back. In this instance, you can assume top level with C:\. It should be noted, that if nothing is found, the string array will be empty.
You have passed a relative file name to Path.GetFullPath. Microsoft documentation states:
If path is a relative path, GetFullPath returns a fully qualified path that can be based on the current drive and current directory. The current drive and current directory can change at any time as an application executes. As a result, the path returned by this overload cannot be determined in advance.
You cannot get the same full path name from a relative path unless your current directory is the same each time you invoke the function.
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
I already know how to browse for an image using open file dialog. So let's say we already got the path :
string imagePath = "Desktop/Images/SampleImage.jpg";
I want to copy that file, into my application folder :
string appFolderPath = "SampleApp/Images/";
How to copy the given image to the appFolderPath programmatically?
Thank you.
You could do something like this:
var path = Path.Combine(
System.AppDomain.CurrentDomain.BaseDirectory,
"Images",
fileName);
File.Copy(imagePath, path);
where fileName is the actual name of the file only (including the extension).
UPDATE: the Path.Combine method will cleanly combine strings into a well-formed path. For example, if one of the strings does have a backslash and the other doesn't it won't matter; they are combined appropriately.
The System.AppDomain.CurrentDomain.BaseDirectory, per MSDN, does the following:
Gets the base directory that the assembly resolver uses to probe for assemblies.
That's going to be the executable path you're running in; so the path in the end (and let's assume fileName is test.txt) would be:
{path_to_exe}\Images\test.txt
string path="Source imagepath";
File.Copy(System.AppDomain.CurrentDomain.BaseDirectory+"\\Images", path);
\ System.AppDomain.CurrentDomain.BaseDirectory is to provide path of the application folder
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 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);