Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Passing variables to Process

Posted on 2014-12-22
3
Medium Priority
?
60 Views
Last Modified: 2014-12-22
Hi

I am passing some parameters to cmd.exe using System.Diagnostics.Process. Parts of the parameters are paths that contain spaces, but I solve that using this syntacs:

string tmpFullPath = @"""C:\My Data\""" + doc + @".xls";

This works fine, but I run into problems if the doc variable contains a filename with spaces.

Can someone please give me the syntacs for how to ‘hide’ the spaces in the doc variables value so that it will work the same way inside the example above?

Regards.
0
Comment
Question by:daghoff
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 40513210
You might want to consider the Path class to generate a file path.
This and the links it contains should help:
http://msdn.microsoft.com/en-us/library/system.io.path.combine%28v=vs.110%29.aspx
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 2000 total points
ID: 40513234
If you've potentially got spaces anywhere in the path, then you'll need to wrap the whole thing in quotes.
Like:
string tmpFullPath = @"""C:\My Data\" + doc + @".xls"""

Open in new window

0
 
LVL 34

Expert Comment

by:it_saige
ID: 40513248
You can use Path.Combine() but can get the same output that you already have.  When passing the path as a process to start you need to add quotes around the path.
using System;
using System.IO;

namespace EE_Q28585330
{
	class Program
	{
		static void Main(string[] args)
		{
			string someDocument = "My Really Long Filename with Spaces";
			string origTmpFullPath = string.Format(@"C:\My Data\{0}.xls", someDocument);
			string combinedTmpFullPath = Path.Combine(@"C:\My Data\", string.Format("{0}.xls", someDocument));
			Console.WriteLine("Original Path = {0}", origTmpFullPath);
			Console.WriteLine("Combined Path = {0}", combinedTmpFullPath);
			Console.WriteLine("What you really need: \"{0}\"", combinedTmpFullPath);
			Console.ReadLine();
		}
	}
}

Open in new window

Produces the following output -Capture.JPG-saige-
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question