In most of my applications (if required) I use the following words: add double quotes at the beginning and end of the line if there are spaces.
public string AddQuotesIfRequired(string path) { return !string.IsNullOrWhiteSpace(path) ? path.Contains(" ") && (!path.StartsWith("\"") && !path.EndsWith("\"")) ? "\"" + path + "\"" : path : string.Empty; }
Examples ..
AddQuotesIfRequired(@"D:\Visual Studio Projects\ProjectOnTFS\ProjectOnTFS");
Returns "D:\Visual Studio Projects\ProjectOnTFS\ProjectOnTFS"
AddQuotesIfRequired(@"C:\Test");
Returns C:\Test
AddQuotesIfRequired(@"""C:\Test Test\Wrap""");
Returns "C:\Test Test\Wrap"
AddQuotesIfRequired(" ");
Returns an empty string
AddQuotesIfRequired(null);
Returns an empty string
EDIT
According to the proposal, the name of the function has changed, and a null check has been added.
A check was added to see if double quotes exist at the beginning and end of a line so as not to duplicate.
Changed the function of checking the string in IsNullOrWhiteSpace to check the space, as well as an empty or null value, which, if so, will return an empty string.
source share