Solved

Passing variables to Process

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

820 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