C# How to use Directory White Spaces into process.arguements?
Asked Answered
A

5

7

The program created utilizes a 3rd party tool to generate a log file.

However the arguments provided for the the tool requires various files from Directory locations as part of generating the logs. Therefore the main argument of @"-r C:\test\ftk\ntuser.dat -d C:\System Volume Information\" + restoreFolder.Name + " -p runmru"; would be used to generate the logs.

May someone advise on how to make the arguments of "C:\System Volume Information\" be processed by the system with the white spaces in placed? Thanks!

The codes:

            Process process = new Process();
            process.StartInfo.FileName = @"C:\test\ftk\ripxp\ripxp.exe";
            process.StartInfo.Arguments = @"-r C:\test\ftk\ntuser.dat -d C:\System Volume Information\" + restoreFolder.Name + " -p runmru";
            process.StartInfo.CreateNoWindow = false;
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.RedirectStandardInput = true;
            process.StartInfo.RedirectStandardError = true;
            process.Start();
Aliment answered 14/12, 2010 at 16:6 Comment(0)
B
10

You need to escape the " by appending a \ to them (\") - for normal strings, or doubling them ("") for verbatim string literals (those starting with @):

process.StartInfo.Arguments = @"-r C:\test\ftk\ntuser.dat -d ""C:\System Volume Information\" + restoreFolder.Name + @""" -p runmru";
Ballentine answered 14/12, 2010 at 16:8 Comment(2)
The restoreFolder.Name is a system name I thinker perhaps you placed an extra " or \ in the wrong area? even the above codes are showing errors already.Aliment
@Aliment - Yeah. You were mixing verbatim string with non verbatim strings... Each needs different escaping. Corrected...Ballentine
C
2

Wrap that path in double quotes:

process.StartInfo.Arguments = @"-r C:\test\ftk\ntuser.dat -d ""C:\System Volume Information\" + restoreFolder.Name + @""" -p runmru";
Cupped answered 14/12, 2010 at 16:8 Comment(1)
The system is showing that there is a unclosed " near the triple """.Aliment
S
1

Perhaps

process.StartInfo.Arguments = @"-r C:\test\ftk\ntuser.dat -d C:\System Volume Information\" + restoreFolder.Name + " -p runmru";

should be

process.StartInfo.Arguments = @"-r ""C:\test\ftk\ntuser.dat"" -d ""C:\System Volume Information\""" + restoreFolder.Name + " -p runmru";
Spindry answered 14/12, 2010 at 16:8 Comment(1)
Reps up for answer but try not to use too much "" cause its confusing.Aliment
J
0

If I correctly understood the question, you can wrap the name with quotes:

"... \"C:\System Volume Information\" + restoreFolder.Name + "\"..."
Jobholder answered 14/12, 2010 at 16:9 Comment(0)
A
0

You really need to use a string.Format along with the Path class:

process.StartInfo.Arguments = @"-r C:\test\ftk\ntuser.dat -d C:\System Volume Information\" + restoreFolder.Name + " -p runmru"

can be rewritten to be a lot cleaner as follows:

string ntuser = @"C:\test\ftk\ntuser.dat";
var args = Path.Combine(@"C:\System Volume Information\", "restoreFolder.Name");

var outs = string.Format("-r {0} -d {1} -p runmru", ntuser, args);
outs.Dump();
Ambrosine answered 14/12, 2010 at 16:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.