I'm creating a process in my application that makes it possible to run commands like you do it in the command prompt.
When I want to export a registry key, I use REG EXPORT path file to access the registry key and export the information into the file. If I use a path which contains no whitespaces at all, everything works fine. But as soon as I use a path with whitespaces, e.g. HKEY_CURRENT_USER\Software\NVIDIA Corporation, it does not work.
I'm appending the path as a String with a StringBuilder to the command, the String itself looks like this: String path = "HKEY_CURRENT_USER\\Software\\NVIDIA Corporation".
Do I have to change the String contained in path? Or is there a special static method I can use to format the String?
You probably need to surround the path with quotes.
This might help:
String path = "\"HKEY_CURRENT_USER\\Software\\NVIDIA Corporation\"";
Related
I have a database table that containing file paths of excel files that I import using a C# script.
The script works fine unless the filepath contains spaces e.g. C:\Temp\My Excel File.xls and I get an Illegal characters in path error message. Unfortunately I am not able to change the file names at the source.
If I hard code the file path to be as below it works fine.
String Filepath = #"C:\Temp\My Excel File.xls";
How do I alter this so I can include a string variable that will store the filepath from the database e.g.
String Filepath = //Code to get FilePath from database
StringCorrectedFilePath = #+FilePath;
Thanks in advance of any help
Edit: Issue is caused by files that start with a number creating invalid escape sequence. e.g. C:\Temp\20160611 My Excel File.xls
Edit 2: SOLVED - Error was caused by carriage return characters appearing after the file extension. Please see my answer for the solution.
Whether you do this
String Filepath = #"C:\Temp\My Excel File.xls";
or this
String Filepath = "C:\\Temp\\My Excel File.xls";
the string stored in memory is just C:\Temp\My Excel File.xls, whatever the debugger may tell you. So when you read some string from somewhere (database, file, user input, ...) you don't need to "escape" backslashes. So just use that string.
Path.GetInvalidFileNameChars
FilePath = string.Concat(FilePath.Split(System.IO.Path.GetInvalidFileNameChars())).Trim();
Well you can replace blank space with %20 character and while retrieving replace back with blank space again like (you may as well choose to use regular expression for the same)
String Filepath = #"C:\Temp\My Excel File.xls";
Filepath = Filepath.Replace(" ", "%20");
While retrieving back
string mypath = pathyouhavegotfromDB.Replace("%20", " ");
I think you need to put quotation marks around the path with spaces.
string filepath = #"C:\Temp\My Excel File.xls";
filepath = $"\"{filepath}\"";
Thanks for everyone's help, I tried all of these and unfortunately they didn't work which led me to believe that the issue wasn't what I originally thought.
It turns out that the files causing the Illegal characters in path all had carriage return characters at the end of the file name, after the file extension.
To resolve this I used the following code and now it works perfectly
FilePath = FilePath.TrimEnd('\r', '\n');
Thanks everyone for your help.
Try this:
String StringCorrectedFilePath = #""+ Filepath;
I have path with backslashes and space that I need to send as parameter to regedit.exe:
\\folder1\folder2\folder three\file.reg
Based on my knowledge using # in front of string should allow back slashes to be specified directly (without escaping). Here is my full code that I am trying to execute it on:
string path = #"\\folder1\folder2\folder three\file.reg"
Process regeditProcess = Process.Start("regedit.exe", file);
When I tried running the program, it gives me an error from regedit's output saying:
Cannot import \folder1\folder2\folder: Error opening the file. There may be a disk or file system error
Since error report backslashes correctly I am guessing the compiler or regedit does not read anything past the whitespace after "folder"
When passing arguments in the command line, you need to surround them with ". Try this:
string path = #"""\\folder1\folder2\folder three\file.reg""";
Process regeditProcess = Process.Start("regedit.exe", path);
Adding "" in a verbatim string will add a single double quote to the string so the resulting string will be "\\folder1\folder2\folder three\file.reg" which will then allow it to get passed correctly to regedit.exe.
I am wondering how to remove the version number from a file path in a Windows Form Application.
Currently I wish to save some users application data to a .xml file located in the roaming user profile settings.
To do this I use:
get
{
return Application.UserAppDataPath + "\\FileName.xml";
}
However this returns the following string:
C:\Users\user\AppData\Roaming\folder\subfolder\1.0.0.0\FileName.xml
and I was wondering if there is a non-hack way to remove the version number from the file path so the file path looks like this:
C:\Users\user\AppData\Roaming\folder\subfolder\FileName.xml
Besides parsing the string looking for the last "\", I do not know what to do.
Thanks
Use Directory.GetParent method for this purpose.
get
{
var dir = Directory.GetParent(Application.UserAppDataPath);
return Path.Combine(dir.FullName, "FileName.xml");
}
Also note that I've used Path.Combine instead of concatenating paths, this method helps you to avoid so many problems. Never concatenate strings to create path.
I want to copy a file to a directory. I thought it would be a simple enough process.
This is the code im using:
string strSrcPath = "C:\Users\Documents\Development\source\11.0.25.10\",
strDstPath = "C:\Users\Documents\Development\testing\11.0.25.10\",
strFile = "BuildLog.txt"
File.Copy(Path.Combine(sourcePath, sourceFile), strDstPath);
The problem here is that when i'm doing the File.Copy it wants to copy one file to another, but I dont want to do that since the file does not exist in the destination path. Therefore I get thrown an error which states something along the lines of 'Cannot copy, strDstPath is a destination not a file"
Was there something I could use instead of File.Copy to copy a file that doesnt exist in the destinaion from the source to destination?
The problem is that the parameters are the source filename and the destination filename. You are passing a destination directory and the program is confused because you can't make the file into a directory.
Use instead:
File.Copy(Path.Combine(strSrcPath , strFile ), Path.Combine(strDstPath, strFile);
You seem to be passing some wrong parameter to the Path.Combine (the second one). It should be strFile instead of sourceFile which is quite unclear where is it coming from.
And you also need to provide a filename for the destination folder:
File.Copy(Path.Combine(sourcePath, strFile), Path.Combine(strDstPath, strFile));
You also need to escape the \ characters in your string because your code will probably not compile. This could be done by either using \\ or by using the # character at the beginning of your string.
string strSrcPath = #"C:\Users\Documents\Development\source\11.0.25.10\",
strDstPath = #"C:\Users\Documents\Development\testing\11.0.25.10\",
strFile = "BuildLog.txt"
File.Copy(Path.Combine(sourcePath, strFile), Path.Combine(strDstPath, strFile));
Also make sure that the destination folder you specified exists. If it doesn't exist you need to create it first (using the Directory.CreateDirectory method).
You have to specify a filename for your destination
so
File.Copy("XMLFile1.xml", #"c:\temp");
will fail where
File.Copy("XMLFile1.xml", #"c:\temp\XMLFile1.xml");
will not
I have a list of files and need to retrieve the filename(s) (Without extension) from file full path.
I generated a list print in command prompt (with path inside intentionally); and I need to replace the path with something else.
i.e:
[This is the *.txt file with generated content example]
C:\folder\files\img1.png
C:\folder\files\img2.png
C:\folder\files\img3.png
C:\folder\files\img4.png
...etc...
What im trying to achieve:
Img1.png. or Img1
I'm using the code bellow, but I don’t know how to use it correctly yet.
[Original Code]
File.WriteAllText("Path", Regex.Replace(File.ReadAllText("Path"), "[Pattern]", "Replacement"));
[The same code; but modified]
File.WriteAllText(#"C:\Folder\Files\print.txt", Regex.Replace(File.ReadAllText(#"C:\Folder\Files\print.txt"), "[C:\folder\files\]", "Copy "));
This is how you can use it. You do not need to use RegEx in this case, because the string which you want to replace is well defined:
File.WriteAllText(#"C:\Folder\Files\print.txt", File.ReadAllText(#"C:\Folder\Files\print.txt").Replace(#"C:\folder\files\", "Copy "));
Couldn't you just use GetFileName and provide the string-value(s)? You can append the output to a string-builder and just rewrite the file.