Solved

Passing variables to Process

Posted on 2014-12-22
3
54 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
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 33

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
2 questions 10 25
C# winforms programmitically move panels 6 29
Please explain the parts of these 2 LINQ expressions 3 33
Winform Module - What is the ASP.Net equiv 2 23
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

832 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