Solved

Passing variables to Process

Posted on 2014-12-22
3
50 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
Comment Utility
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
Comment Utility
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 32

Expert Comment

by:it_saige
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

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…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

772 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now