We help IT Professionals succeed at work.

How to format Process.StartInfo.Arguments with spaces in the arguments?

2,422 Views
Last Modified: 2013-12-09
hi,

I am trying to invoke an ftp command from my C# application using a Process.
I have the below code in my program.
I have spaces in the first argument of the Process and it is acting weird and not formatting properly.

scriptfile = "C:\Program files\PR Ent\AddIns\PR Bridge\Upload\UploadScript.txt";
s.Filename = "File1";

Process P = new Process();
                    P.EnableRaisingEvents = true;
                    P.Exited += P_Exited;
                    P.StartInfo.FileName = filename;
                    P.StartInfo.Arguments = scriptfile + s.Filename;
                    P.Start();

Could anybody please help me how to format properly when having spaces?

Thank you.
Comment
Watch Question

Sr. System Analyst
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
HainKurtSr. System Analyst
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Thanks for the response.

Can you please clarify why we need to change

scriptfile = "C:\Program files\PR Ent\AddIns\PR Bridge\Upload\UploadScript.txt";
-->
scriptfile = "C:\\Program files\\PR Ent\\AddIns\\PR Bridge\\Upload\\UploadScript.txt";

Also, Would the below line is considered as having two arguments?

P.StartInfo.Arguments = "\"" + scriptfile +"\" "+ s.Filename;

Thanks.

Author

Commented:
For some reason it is still having the same problem.

In my script I have the following code. When this script is executed it is supposed to create a file in the below location. But it is creating a file in "C:\Program" since it is taking the file name only up to the first space.

C:\\Program files\\PR Ent\\AddIns\\PR Bridge\\Upload\\UploadScript.txt

Thanks for your help.
@echo off
setlocal
set ftpscript=%1
echo open ftp.coinc.com>> "%ftpscript%"
echo co-ftp>> "%ftpscript%"
echo pw-ftp>> "%ftpscript%"
echo lcd C:\Program Files\PR Ent\AddIns\PR Bridge\Upload>> "%ftpscript%"
echo put %2>> "%ftpscript%"
echo disconnect>> "%ftpscript%"
echo bye>> "%ftpscript%"

ftp -v -i -s:%ftpscript%

Open in new window

HainKurtSr. System Analyst
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Thank you for the responses.
I have been trying to get it worked for several hours but still having a problem.
I am trying exactly the attached code. And I have my Upload.cmd as below.
It is creating the below "UploadScript.txt" as expected in the correct location "C:\Temp Files". But it is saying "Error opening script file C:\Temp"


@echo off
setlocal
set ftpscript="%1"
echo open ftp.coinc.com>> "%ftpscript%"
echo co-ftp>> "%ftpscript%"
echo pw-ftp>> "%ftpscript%"
echo lcd "C:\Temp Files\UploadFiles">> "%ftpscript%"
echo put %2>> "%ftpscript%"
echo disconnect>> "%ftpscript%"
echo bye>> "%ftpscript%"

ftp -v -i -s:"%ftpscript%"


UploadScript.txt is created as follows and looks good. But for some reason it is still trying to look for C:\Temp

open ftp.coinc.com
co-ftp
pw-ftp
lcd "C:\Temp Files\UploadFiles"
put G3A336
disconnect
bye


Thanks for your help.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace seasharpwinapp
{
    public partial class Form1 : Form
    {

        string batchscriptfile = @"C:\Temp Files\UploadScript.txt";
        string filename = "G3A336";

        bool hasEventException = false;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            bool status = StartUploadProcess();

            if (status)
            {
                MessageBox.Show("Upload successful");
            }
            else
            {
                MessageBox.Show("Upload failed");
            }
        }
        
        private bool StartUploadProcess()
        {
            try
            {
                Process P = new Process();
                P.EnableRaisingEvents = true;
                
                P.StartInfo.FileName = @"C:\\Temp Files\\Upload.cmd";
                
                P.StartInfo.Arguments = string.Format("\"{0}\" {1}", batchscriptfile, filename);
                
                P.Start();
                System.Threading.Thread.Sleep(20000);


                return !hasEventException;               
            }


            catch (Exception x)
            {
                MessageBox.Show(string.Format("Error while uploading. {0} \r\n {1}", x.Message, x.InnerException.ToString()));
                return false;
            }
        }

        
        
    }
}

Open in new window

Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Commented:
Complete upload.cmd:
@echo off
setlocal
set ftpscript=%1
echo open ftp.coinc.com>> "%ftpscript%"
echo co-ftp>> "%ftpscript%"
echo pw-ftp>> "%ftpscript%"
echo lcd "C:\Temp Files\UploadFiles">> "%ftpscript%"
echo put %2>> "%ftpscript%"
echo disconnect>> "%ftpscript%"
echo bye>> "%ftpscript%"

ftp -v -i -s:%ftpscript%

Open in new window

HainKurtSr. System Analyst
CERTIFIED EXPERT

Commented:
try single quotes

echo lcd "C:\Temp Files\UploadFiles">> "%ftpscript%"
-->
echo lcd 'C:\Temp Files\UploadFiles'>> "%ftpscript%"

or use 8 char dir name

echo lcd C:\TEMPFI~1\UPLOAD~1>> "%ftpscript%"

Commented:
HainKurt:
I have reproduce the problem locally and my updated upload.cmd works fine.

Author

Commented:
Please give me a little bit time. I am trying and let you know.

Thanks guys for your help.

Author

Commented:
Please give me a little bit time. I am trying and let you know.

Thanks guys for your help.
HainKurtSr. System Analyst
CERTIFIED EXPERT

Commented:
MikeQc,

what is your explanation for this?

"
UploadScript.txt is created as follows and looks good. But for some reason it is still trying to look for C:\Temp

open ftp.coinc.com
co-ftp
pw-ftp
lcd "C:\Temp Files\UploadFiles"
put G3A336
disconnect
bye
"

Author

Commented:
Hi Guys,

It is working now. I had to remove the quotes in each line of the Upload.cmd script.
The script looks as follows and I did not have to change anything else in the C# program.
This line still works.
P.StartInfo.Arguments = string.Format("\"{0}\" {1}", batchscriptfile, filename);

echo off
setlocal
set ftpscript=%1
echo open ftp.coinc.com>> %ftpscript%
echo co-ftp>> %ftpscript%
echo pw-ftp>> %ftpscript%
echo lcd "C:\Temp Files\UploadFiles">> %ftpscript%
echo put %2>> %ftpscript%
echo disconnect>> %ftpscript%
echo bye>> %ftpscript%

ftp -v -i -s:%ftpscript%


I appreciate both of your help on this. Thank you very much.

Author

Commented:
Thank you.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.