Smile Azeez
Smile Azeez

Reputation: 147

How to remove spaces in path?

i have to start process which is placed inside Program Files. But the problem is that Process.Start does not taking space in path.

Process regeditProcess = Process.Start("regedit.exe", "/s C:\\Program Files\\Test Folder\\sample.reg");

Path:

C:\\Program Files\\Test Folder\\sample.reg

there is a space between Program and Files in 'Program Files'. Thats my problem. How to avoid space?

Upvotes: 1

Views: 7805

Answers (4)

Jeff Mercado
Jeff Mercado

Reputation: 134841

The proper thing to do would be to quote the path that contains spaces. So the argument string should be like this:

"/s \"C:\\Program Files\\Test Folder\\sample.reg\""

Though when working with paths, you generally should always use verbatim literal strings.

@"/s ""C:\Program Files\Test Folder\sample.reg"""

Otherwise, you could convert the path using 8.3 names. I don't know of any methods to do this for you in the framework but the rules are simple. If you have a long name that is longer than 6 characters, you take the first 6 non-space characters and append it with tilde (~) followed by a number (usually starting with 1). If multiple files have the same 6 characters, the number is incremented in alphabetical order. So in your case it could be written:

@"/s C:\Progra~1\TestFo~1\sample.reg"

Upvotes: 1

Anuraj
Anuraj

Reputation: 19598

You can do something like this to get Program files

Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFiles)

Here is more detailed code

if(Environment.Is64BitOperatingSystem)
{
Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFilesX86)
}
else
{
Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFiles)
}

Upvotes: 1

Botz3000
Botz3000

Reputation: 39610

Process.Start is not the problem here, the problem is that regedit.exe doesn't accept spaces in the parameter. Put it into quotes:

Process.Start("regedit.exe", "/s \"C:\\Program Files\\Test Folder\\sample.reg\"");

also, you should use %ProgramFiles% or something equivalent to get the program files folder instead of hardcoding "C:\\Program Files".

Upvotes: 3

Dennis
Dennis

Reputation: 37770

You should pass command line arguments, containing spaces, in quotes ("), like this:

Process regeditProcess = Process.Start("regedit.exe", "/s \"C:\\Program Files\\Test Folder\\sample.reg\"");

Upvotes: 4

Related Questions