473,397 Members | 2,028 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Problem with System.Process and quotes/spaces in arguments

I wrote a console application that basically consumes arguments and starts
other command line apps via System.Process. Let's call it XCompile for now.
I wrote a Visual basic add-in that does pretty much the same thing to
XCompile. Let's call it MyAddin.

XCompile collects information to send to vbc.exe. When it comes across any
arguments that are file paths, it wraps them in quotes. For example:

[VB.Net psuedocode (from XCompile)]
Dim prms As String
Dim fil As String
Dim files() As String = New String() {"c:\Documents and Settings\...\file
with spaces.vb", "temp\NoSpaces.vb"}

For Each fil In files
prms &= " "
prms &= """"c
prms &= fil
prms &= """"c
Next

Console.WriteLine("vbc.exe " & Trim(prms))

Dim si As New ProcessStartInfo("vbc.exe", Trim(prms))
si.UseShellExecute = False
si.CreateNoWindow = True
si.RedirectStandardOutput = True
si.RedirectStandardErr = True
Dim prc As Process = Process.Start(si)
....

This works well. The problem I come across is when I try to use a Process
against XCompile in the same fashion. vbvc pretends that all the quotes have
disappeared and treats everything with a space in it as a separate argument.

[VB.Net psuedocode (from MyAddin)]
Dim si As New ProcessStartInfo("XCompile.exe", Trim(prms))
si.UseShellExecute = False
si.CreateNoWindow = True
si.RedirectStandardOutput = True
si.RedirectStandardErr = True
Dim prc As Process = Process.Start(si)
....

[Output from XCompile]
vbc.exe "c:\Documents and Settings\...\file with spaces.vb"
"temp\NoSpaces.vb"
vbc : Command line error BC2001 : file 'and' could not be found
vbc : Command line error BC2001 : file 'Settings\waldo\My' could not be
found
....
vbc : Command line error BC2008 : no input sources specified

So in summary, XCompile works, shelling to vbc by itself, but doesn't work
when MyAddin shells to XCompile.
Any Ideas why this happens/how to fix it?

Thanks in advance.
WALDO
Nov 21 '05 #1
7 6975
You said that XCompile adds quotation marks to any file paths. I
notice also, that your pseudocode is adding quotation marks.

Could there be extra quotation marks in the command string? In other
words, is the command string somehow ending up like the following:

""c:\Documents and Settings\waldo\My ...""
Note the extra quotation marks on the ends.

Just a thought

Nov 21 '05 #2
I'm not familar enough with VB to understand some of that code. I'll just say
that I wrote a routine I call "DoubleEscape" (escape by doubling) that I use
in similar situations, passing strings containing quotes around (mostly to
ADO.net).

It turns: They call me "Ishmael".
To: They call me ""Ishmael"".

Which is often needed when I build SQL statements.

You pass in a string to work on and a string containing the characters that
need to be doubled (usually quotes), e.g.

newstring = DoubleEscape ( oldstring , "\"" ) ;

I also have a routine AddEscape that is similar but could make the above
string into:
They call me \"Ishmael\".

The C# code for these is:

public static string
DoubleEscape
(
string subject ,
string problems
)
{
string result = "" ;

for ( int runner = 0 ; runner < subject.Length ; runner++ )
{
if ( problems.IndexOf ( subject [ runner ] , 0 ,
problems.Length ) != -1 )
{
result += subject [ runner ] ;
}

result += subject [ runner ] ;
}

return ( result ) ;
}

public static string
AddEscape
(
string subject ,
string problems ,
char escape
)
{
string result = "" ;

for ( int runner = 0 ; runner < subject.Length ; runner++ )
{
if ( problems.IndexOf ( subject [ runner ] , 0 ,
problems.Length ) != -1 )
{
result += escape ;
}

result += subject [ runner ] ;
}

return ( result ) ;
}
Nov 21 '05 #3
The pseudocode IS XCompile. Not the pseudocode is adding quotes AND XComile
is adding quotes.

I actually tried doing that on puropse (doubling up on qoutes when not run
from the command line), but it didn't work.

"Chris Dunaway" <du******@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
You said that XCompile adds quotation marks to any file paths. I
notice also, that your pseudocode is adding quotation marks.

Could there be extra quotation marks in the command string? In other
words, is the command string somehow ending up like the following:

""c:\Documents and Settings\waldo\My ...""
Note the extra quotation marks on the ends.

Just a thought

Nov 21 '05 #4
I tried double escaping my quotes when not running from the command line. no
joy.

"PIEBALD" <PI*****@discussions.microsoft.com> wrote in message
news:2C**********************************@microsof t.com...
I'm not familar enough with VB to understand some of that code. I'll just say that I wrote a routine I call "DoubleEscape" (escape by doubling) that I use in similar situations, passing strings containing quotes around (mostly to
ADO.net).

It turns: They call me "Ishmael".
To: They call me ""Ishmael"".

Which is often needed when I build SQL statements.

You pass in a string to work on and a string containing the characters that need to be doubled (usually quotes), e.g.

newstring = DoubleEscape ( oldstring , "\"" ) ;

I also have a routine AddEscape that is similar but could make the above
string into:
They call me \"Ishmael\".

The C# code for these is:

public static string
DoubleEscape
(
string subject ,
string problems
)
{
string result = "" ;

for ( int runner = 0 ; runner < subject.Length ; runner++ )
{
if ( problems.IndexOf ( subject [ runner ] , 0 ,
problems.Length ) != -1 )
{
result += subject [ runner ] ;
}

result += subject [ runner ] ;
}

return ( result ) ;
}

public static string
AddEscape
(
string subject ,
string problems ,
char escape
)
{
string result = "" ;

for ( int runner = 0 ; runner < subject.Length ; runner++ )
{
if ( problems.IndexOf ( subject [ runner ] , 0 ,
problems.Length ) != -1 )
{
result += escape ;
}

result += subject [ runner ] ;
}

return ( result ) ;
}

Nov 21 '05 #5
So I thought, "Try to eliminate the problem." I merged XCompile and MyAddin
into one app and found a whole slew of new problems. Rather than having
MyAddin shell to XCompile and XCompile shell to vbc, MyAddin does all the
work of XCompile (exact same code).

I got the exact same problem. It seems this funky behavior is produced when
it's a NON command-line app (or at least an add-in). Running XCompile by
itself doesn't have a problem. Running MyAddin, whether it shells to
XCompile or does XCompile's work (shelling to vbc), seems to have the same
effect. The question is "Is it because it's an Add-In (something to do with
COM interop, maybe?) or is it because it's non-commandline (WINEXE/LIBRARY
instead of EXE)?"

If I run XCompile, or even MyAddin, from the command-line or double-clicking
it, it compiles successfully using the quotes the exact same.

In addition to that, I've noticed that if you actually run command-line
exe's from the command line instead of double-clicking them,
Console.WriteLine doesn't produce anything. Why is that?

"WALDO" <NO****@NOSPAM.COM> wrote in message
news:%2***************@TK2MSFTNGP09.phx.gbl...
I wrote a console application that basically consumes arguments and starts
other command line apps via System.Process. Let's call it XCompile for now. I wrote a Visual basic add-in that does pretty much the same thing to
XCompile. Let's call it MyAddin.

XCompile collects information to send to vbc.exe. When it comes across any
arguments that are file paths, it wraps them in quotes. For example:

[VB.Net psuedocode (from XCompile)]
Dim prms As String
Dim fil As String
Dim files() As String = New String() {"c:\Documents and Settings\...\file
with spaces.vb", "temp\NoSpaces.vb"}

For Each fil In files
prms &= " "
prms &= """"c
prms &= fil
prms &= """"c
Next

Console.WriteLine("vbc.exe " & Trim(prms))

Dim si As New ProcessStartInfo("vbc.exe", Trim(prms))
si.UseShellExecute = False
si.CreateNoWindow = True
si.RedirectStandardOutput = True
si.RedirectStandardErr = True
Dim prc As Process = Process.Start(si)
...

This works well. The problem I come across is when I try to use a Process
against XCompile in the same fashion. vbvc pretends that all the quotes have disappeared and treats everything with a space in it as a separate argument.
[VB.Net psuedocode (from MyAddin)]
Dim si As New ProcessStartInfo("XCompile.exe", Trim(prms))
si.UseShellExecute = False
si.CreateNoWindow = True
si.RedirectStandardOutput = True
si.RedirectStandardErr = True
Dim prc As Process = Process.Start(si)
...

[Output from XCompile]
vbc.exe "c:\Documents and Settings\...\file with spaces.vb"
"temp\NoSpaces.vb"
vbc : Command line error BC2001 : file 'and' could not be found
vbc : Command line error BC2001 : file 'Settings\waldo\My' could not be
found
...
vbc : Command line error BC2008 : no input sources specified

So in summary, XCompile works, shelling to vbc by itself, but doesn't work
when MyAddin shells to XCompile.
Any Ideas why this happens/how to fix it?

Thanks in advance.
WALDO

Nov 21 '05 #6
My own stupidity prevails sometimes.
I compiled MyAddin as a windows exe which is why I wasn't getting output
from the command line.
I wonder if that solves my vbc problem.

"WALDO" <NO****@NOSPAM.COM> wrote in message
news:ux**************@TK2MSFTNGP14.phx.gbl...
So I thought, "Try to eliminate the problem." I merged XCompile and MyAddin into one app and found a whole slew of new problems. Rather than having
MyAddin shell to XCompile and XCompile shell to vbc, MyAddin does all the
work of XCompile (exact same code).

I got the exact same problem. It seems this funky behavior is produced when it's a NON command-line app (or at least an add-in). Running XCompile by
itself doesn't have a problem. Running MyAddin, whether it shells to
XCompile or does XCompile's work (shelling to vbc), seems to have the same
effect. The question is "Is it because it's an Add-In (something to do with COM interop, maybe?) or is it because it's non-commandline (WINEXE/LIBRARY
instead of EXE)?"

If I run XCompile, or even MyAddin, from the command-line or double-clicking it, it compiles successfully using the quotes the exact same.

In addition to that, I've noticed that if you actually run command-line
exe's from the command line instead of double-clicking them,
Console.WriteLine doesn't produce anything. Why is that?

"WALDO" <NO****@NOSPAM.COM> wrote in message
news:%2***************@TK2MSFTNGP09.phx.gbl...
I wrote a console application that basically consumes arguments and starts other command line apps via System.Process. Let's call it XCompile for

now.
I wrote a Visual basic add-in that does pretty much the same thing to
XCompile. Let's call it MyAddin.

XCompile collects information to send to vbc.exe. When it comes across any arguments that are file paths, it wraps them in quotes. For example:

[VB.Net psuedocode (from XCompile)]
Dim prms As String
Dim fil As String
Dim files() As String = New String() {"c:\Documents and Settings\...\file with spaces.vb", "temp\NoSpaces.vb"}

For Each fil In files
prms &= " "
prms &= """"c
prms &= fil
prms &= """"c
Next

Console.WriteLine("vbc.exe " & Trim(prms))

Dim si As New ProcessStartInfo("vbc.exe", Trim(prms))
si.UseShellExecute = False
si.CreateNoWindow = True
si.RedirectStandardOutput = True
si.RedirectStandardErr = True
Dim prc As Process = Process.Start(si)
...

This works well. The problem I come across is when I try to use a Process against XCompile in the same fashion. vbvc pretends that all the quotes

have
disappeared and treats everything with a space in it as a separate

argument.

[VB.Net psuedocode (from MyAddin)]
Dim si As New ProcessStartInfo("XCompile.exe", Trim(prms))
si.UseShellExecute = False
si.CreateNoWindow = True
si.RedirectStandardOutput = True
si.RedirectStandardErr = True
Dim prc As Process = Process.Start(si)
...

[Output from XCompile]
vbc.exe "c:\Documents and Settings\...\file with spaces.vb"
"temp\NoSpaces.vb"
vbc : Command line error BC2001 : file 'and' could not be found
vbc : Command line error BC2001 : file 'Settings\waldo\My' could not be
found
...
vbc : Command line error BC2008 : no input sources specified

So in summary, XCompile works, shelling to vbc by itself, but doesn't work when MyAddin shells to XCompile.
Any Ideas why this happens/how to fix it?

Thanks in advance.
WALDO


Nov 21 '05 #7
I'll be damned. It solved my problem

:|

"WALDO" <NO****@NOSPAM.COM> wrote in message
news:uM**************@TK2MSFTNGP15.phx.gbl...
My own stupidity prevails sometimes.
I compiled MyAddin as a windows exe which is why I wasn't getting output
from the command line.
I wonder if that solves my vbc problem.

"WALDO" <NO****@NOSPAM.COM> wrote in message
news:ux**************@TK2MSFTNGP14.phx.gbl...
So I thought, "Try to eliminate the problem." I merged XCompile and

MyAddin
into one app and found a whole slew of new problems. Rather than having
MyAddin shell to XCompile and XCompile shell to vbc, MyAddin does all the
work of XCompile (exact same code).

I got the exact same problem. It seems this funky behavior is produced

when
it's a NON command-line app (or at least an add-in). Running XCompile by
itself doesn't have a problem. Running MyAddin, whether it shells to
XCompile or does XCompile's work (shelling to vbc), seems to have the same effect. The question is "Is it because it's an Add-In (something to do

with
COM interop, maybe?) or is it because it's non-commandline (WINEXE/LIBRARY instead of EXE)?"

If I run XCompile, or even MyAddin, from the command-line or

double-clicking
it, it compiles successfully using the quotes the exact same.

In addition to that, I've noticed that if you actually run command-line
exe's from the command line instead of double-clicking them,
Console.WriteLine doesn't produce anything. Why is that?

"WALDO" <NO****@NOSPAM.COM> wrote in message
news:%2***************@TK2MSFTNGP09.phx.gbl...
I wrote a console application that basically consumes arguments and starts other command line apps via System.Process. Let's call it XCompile for

now.
I wrote a Visual basic add-in that does pretty much the same thing to
XCompile. Let's call it MyAddin.

XCompile collects information to send to vbc.exe. When it comes across any arguments that are file paths, it wraps them in quotes. For example:

[VB.Net psuedocode (from XCompile)]
Dim prms As String
Dim fil As String
Dim files() As String = New String() {"c:\Documents and Settings\...\file with spaces.vb", "temp\NoSpaces.vb"}

For Each fil In files
prms &= " "
prms &= """"c
prms &= fil
prms &= """"c
Next

Console.WriteLine("vbc.exe " & Trim(prms))

Dim si As New ProcessStartInfo("vbc.exe", Trim(prms))
si.UseShellExecute = False
si.CreateNoWindow = True
si.RedirectStandardOutput = True
si.RedirectStandardErr = True
Dim prc As Process = Process.Start(si)
...

This works well. The problem I come across is when I try to use a Process against XCompile in the same fashion. vbvc pretends that all the quotes have
disappeared and treats everything with a space in it as a separate

argument.

[VB.Net psuedocode (from MyAddin)]
Dim si As New ProcessStartInfo("XCompile.exe", Trim(prms))
si.UseShellExecute = False
si.CreateNoWindow = True
si.RedirectStandardOutput = True
si.RedirectStandardErr = True
Dim prc As Process = Process.Start(si)
...

[Output from XCompile]
vbc.exe "c:\Documents and Settings\...\file with spaces.vb"
"temp\NoSpaces.vb"
vbc : Command line error BC2001 : file 'and' could not be found
vbc : Command line error BC2001 : file 'Settings\waldo\My' could not
be found
...
vbc : Command line error BC2008 : no input sources specified

So in summary, XCompile works, shelling to vbc by itself, but doesn't

work when MyAddin shells to XCompile.
Any Ideas why this happens/how to fix it?

Thanks in advance.
WALDO



Nov 21 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my...
0
by: Elmar Krieger | last post by:
Hi! The _spawnvp function expects the command line arguments as a NULL terminated list of char * pointers, hence there is no need to enclose filenames with quotes if they contain spaces....
1
by: ginee lee via .NET 247 | last post by:
(Type your message here) hi all, It seems that the arguments of System.Diagnostics.Process.Start()can only be the absolute path. The args can not be like".\abc\efg.exe" or "..\abc\efg.exe". While i...
11
by: Dyl | last post by:
Hi all, I am having a problem with a command prompt process. It runs the cmd prompt sucessfully, and changes the directory succesfully, but I can't get the StartInfo.Arguments to work properly....
9
by: Sam Marrocco | last post by:
I'm using a ProcessStartInfo object to provide command line information to a process for execution as follows: Dim PSI as new ProcessStartInfo PSI.Filename MyApp.exe PSI.Arguments -blah -blah...
0
by: WALDO | last post by:
I wrote a console application that basically consumes arguments and starts other command line apps via System.Process. Let's call it XCompile for now. I wrote a Visual basic add-in that does pretty...
5
by: Reddy | last post by:
I am running process.sart to run gpg.exe to encrypt files. It's working fine with console application. When i use same code in windows service. It's not working. It's not even throwing...
6
by: kimiraikkonen | last post by:
Hello, I want to ask this: If i do: System.Diagnostics.Process.Start("c:\lame", "--preset standard c:\blabla.wav c:\blabla.mp3") it works. But i don't want this. I want my 2 textboxes must...
12
by: tom_kuehnert | last post by:
Hi! I'm trying to execute a program using system(). The program itself is located in some path which might contain whitespaces. Simple solution would be this: system("\"C:\A B\C.exe\""); ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.