Solved

Passing variables to Process

Posted on 2014-12-22
3
56 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 44

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 500 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Suggested Solutions

Title # Comments Views Activity
How to identify a paragraph in the body of text? 2 74
How to force output to ascii 2 57
Visual Studio 2015 auto inserted code 12 85
Web page design problem 3 37
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

740 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