Resolved Trying to send a string to a console application with whitespaces.

ProtekNickz

Member
Joined
Jan 13, 2022
Messages
14
Programming Experience
5-10
Hi, I have Googled until my eyes bleed , I have two programs I'm working on, One is a Desktop Forms Application and the other is a Console Application, I'm trying to pass a string to the console app as an argument which is got from the Desktop Application from is

Strings:
MyStrings = "Block" + " "  + _RuleName + " " + __Description + " " + _RemoteAddress + " " + MyFileDialog.FileName;

When I send this string to the Console Application the first four arguments are accepted fine, but when it reaches the MyFileDialog.FileName, Example which should look like "C:\Program Files\My Application\Application Name.exe" from the MyFileDialog.FileName, but when it sends the string to the Console Application it only comes in to the argument[0] like "C:\Program Files\My" as it dismisses the rest after the white spaces, basically it trunacats my string, I've tried various things like ' some String ' and "' Some string '" or even @" some string ", I'm thinking its on the Console Applications side due to the arguments, if so how can i send a string to a Console Argument with spaces in it from a OpenFileDialog box?.

Cheers in advance ProtekNickz! :)
 
Show us how your are trying to send it.

And show us how your console app is parsing its arguments.
 
The reason why I'd like to see both sides, particularly the console side, is because you said:
it only comes in to the argument[0] like "C:\Program Files\My" as it dismisses the rest after the white spaces,

It should have come into argument[4] and only as "C:\Program".

In general the correct fix is to have the sender to put any strings with whitespaces inside quotes, and the receiver trust the framework to correctly parse the arguments (e.g. arguments in quotes will be treated as a single argument).
 
Last edited:
Firstly, NEVER concatenate two string literals. It's madness.
C#:
MyStrings = "Block "  + _RuleName + " " + __Description + " " + _RemoteAddress + " " + MyFileDialog.FileName;
Secondly, while it's not wrong to use multiple concatenation operators, string interpolation will make your code more readable:
C#:
MyStrings = $"Block {_RuleName} {__Description} {_RemoteAddress} {MyFileDialog.FileName}";
That's more readable even without quoting the arguments but if you do quote the arguments the difference will be even greater, because of all the extra quotes:
C#:
MyStrings = $"Block {_RuleName} {__Description} {_RemoteAddress} \"{MyFileDialog.FileName}\"";
or, if any of the others might also contain spaces:
C#:
MyStrings = $"Block \"{_RuleName}\" \"{__Description}\" \"{_RemoteAddress}\" \"{MyFileDialog.FileName}\"";
 
Thanks for all the help people, With your help and more looking more in to what was said gave my brain a kick in the right gear, also on the Console side of things I'm using

Strings:
//Add this in both Solutions.
using System.Text.RegularExpressions;

//In the argument receive.
_RecieveDescription = Regex.Replace(args[2], "-", " ");

//And in the Desktop app, I'm sending the strings with spaces replaced with "-"
//It just makes life easier.

//In The Buttonpress Send.
String Mystring = Openfiledialog.Filename;
_SentDescription = Regex.Replace(args[2], " ", "-");

//Hope this helps anyone if their also looking.

This was my Solution, It might not be great but seems to work great.
 
Last edited:
And what happens if the string being sent is "mother-in-law.exe" ? How will your console code know not to replace the dashes that are really supposed to be there?
 
And what happens if the string being sent is "mother-in-law.exe" ? How will your console code know not to replace the dashes that are really supposed to be there?
I've already thought of that, i was using "-" as an example, I'm actually going to use "•" or maybe some other ASCII none standard so it can't be used on regular file names or folders, but still usable via code.
 
I've already thought of that, i was using "-" as an example, I'm actually going to use "•" or maybe some other ASCII none standard so it can't be used on regular file names or folders, but still usable via code.
Be aware that NTFS uses Unicode, not ASCII so you'll have to choose wisely among characters that are not legal for use in filenames.

Using quotes around the parameters would have been much better approach.
 
Back
Top Bottom