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.
Related
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\"";
I have the problem with whitespaces in my console application. My application is crashing because of not allowed chararcters.
My application is expecting an argument which is a path of the filesystem. So it will be called like this
myProg.exe "D:\tmp\with whitespace\"
With that information I want to create a file in the given folder, but this is not possible because of not allowed char.
String pdfName = "foobar.pdf
String datapath = args[0];
String targetJobFile = datapath + pdfName + ".txt";
I can see in the debugger that the targetJobFile value is
"D:\tmp\with whitespace\"foobar.pdf.txt
And there i receive the exception.
Thanks
You need to remove the quotes from the argument, so before using it, use Trim:
String realArg = args[0].Trim('"');
You already have the \ that Path.Combine would give you; but if you don't want your users to have to enter it, using Path.Combine is a good way to get the path separator character into your string.
Why not just check for existence of " character and replace them before making the full path like
datapath = datapath.Replace("\"","");
String targetJobFile = datapath + pdfName;
You can go one step ahead and use Path.GetInvalidPathChars() which will return all invalid characters not allowed in path names and remove it from your path as follows -
modifiedPathName = new Regex(string.Format("[{0}]", Regex.Escape(new string(Path.GetInvalidPathChars())))).Replace(PathName, replaceChar);
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 string that is in the registry that can either point to an .exe, .txt along with certain command line arguments like:
C:\\PAthtoTheExe\program1.exe -arg1 -arg2
How do I determine if the "program1.exe" is a valid executable, IE that it points to an existing file? (this could be removed if the user uninstalls the application for eg:)
I don't like the idea of parsing out the arguments, because right now, the path can either point to an .exe or .txt or a .url. I would have to put in a long if else to do that.
There's a
File.Exists( path );
method that will return true if the filename exists. It won't tell you if the file contains a valid executable, though.
You can separate the path from the other arguments using
string tokens[] = registry_value.Split( ' ' );
then passing the first token to File.Exists()
hi for the following code, why is it that i am getting this output in the error text file?
"'ha57061' is not recognized as an internal or external command,
operable program or batch file."
my user name is cha57061. why am i missing the "c" and " ' "? please correct me if my code is wrong.
System.Diagnostics.Process runantc = new System.Diagnostics.Process();
runantc.StartInfo.FileName = "C:/Documents and Settings/Cha57061/Desktop/New Folder/WPF/WpfApplication1/WpfApplication1/cmd.exe";
runantc.StartInfo.Arguments = "antc.bat";
runantc.StartInfo.UseShellExecute = false;
runantc.StartInfo.RedirectStandardOutput = true;
runantc.StartInfo.RedirectStandardError = true;
runantc.Start();
string procOutput = runantc.StandardOutput.ReadToEnd();
string procError = runantc.StandardError.ReadToEnd();
TextWriter outputlog = new StreamWriter("C:/Documents and Settings/Cha57061/Desktop/New Folder/WPF/WpfApplication1/WpfApplication1/processoutput.txt");
outputlog.Write(procOutput);
outputlog.Close();
TextWriter outputerror = new StreamWriter("C:/Documents and Settings/Cha57061/Desktop/New Folder/WPF/WpfApplication1/WpfApplication1/error.txt");
outputerror.Write(procError);
outputerror.Close();
Not sure if it solves your problem, but this is the first time i'm seeing file paths in C#.NET using forward slashes (/), not sure if they get converted to (\) automatically.
You might try rewriting your paths as indicated below
"C:\\Directory\\File" //the double slash is necessary since (\) indicates an escape character is to come)
#"C:\Directory\" // the # modifier changes the default behavior and escape characters are not considered the same way
\c is a escape character, as described here: http://msdn.microsoft.com/en-us/library/4edbef7e%28v=vs.71%29.aspx
That's the wrong way to run a batch file.
You should set FileName directly to the path to your .bat file.
To answer your question, your batch file has a problem.