Putting quotes on a statement

Im using the following code and I need to be able to put quotes in the following statement.   The proper command to be enter is this

Netsh.exe firewall add portopening TCP 1212 “Add Quotes”

This is the programmatic line im using

p.StartInfo.Arguments = "firewall add portopening TCP " + textBox3.Text + "/" + textBox5.Text;

Just not sure how to add what I need to the input on textBox5



Process p = new Process();
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.CreateNoWindow = true;
            p.StartInfo.FileName = "netsh.exe";
            p.StartInfo.Arguments = "firewall add portopening TCP " + textBox3.Text + "/" + textBox5.Text;
            p.Start();
PeterSchenkAsked:
Who is Participating?
 
Éric MoreauSenior .Net ConsultantCommented:
so it should be:
p.StartInfo.Arguments = "firewall add portopening TCP " + textBox3.Text + " " + Convert.ToChar(34) + textBox5.Text+ Convert.ToChar(34);
0
 
Éric MoreauSenior .Net ConsultantCommented:
you can do something like this:
string Arguments = "firewall add portopening TCP " + tb3 + "/" + Convert.ToChar(34) + tb5 + Convert.ToChar(34);
0
 
PeterSchenkAuthor Commented:
Same issue did not work
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
AndyAinscowFreelance programmer / ConsultantCommented:
put a breakpoint on the line p.Start(); and inspect what the StartInfo.Arguments actually are.
0
 
Éric MoreauSenior .Net ConsultantCommented:
can you show us your code ?

why do you have a / in your string concatenation?
0
 
PeterSchenkAuthor Commented:
Did all that....

Need the qoutes around the name of the port.... which is a textbox.

Netsh.exe firewall add portopening TCP 1212 “Port Name”

0
 
PeterSchenkAuthor Commented:
Here is all the code

       private void button3_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(textBox3.Text) || String.IsNullOrEmpty(textBox5.Text))
            {
                MessageBox.Show("TCP Port Number and Port Name must be filled in!", "Error");
                return;
            }

            Process p = new Process();
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.CreateNoWindow = true;
            p.StartInfo.FileName = "netsh.exe";
            p.StartInfo.Arguments = "firewall add portopening TCP " + textBox3.Text + "/" + textBox5.Text;
            p.Start();

            // Read the output stream first and then wait.
            string output = p.StandardOutput.ReadToEnd();
            p.WaitForExit();

            MessageBox.Show(output, "Result");
            Application.Exit();
        }
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
What was the value of p.StartInfo.Arguments ?
0
 
Éric MoreauSenior .Net ConsultantCommented:
change:
p.StartInfo.Arguments = "firewall add portopening TCP " + textBox3.Text + "/" + textBox5.Text;

to:
p.StartInfo.Arguments = "firewall add portopening TCP " + textBox3.Text + "/" + Convert.ToChar(34) + textBox5.Text+ Convert.ToChar(34);
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
Also why have you got a "/" in the middle of the string you construct when that doesn't appear in what you say you require as a string?
0
 
PeterSchenkAuthor Commented:
emoreua,
If I make that change the string "output" contains the following information

output = "The syntax supplied for this command is not valid. Check help for the correct syntax.\r\n\r\nadd portopening\r\n      [ protocol = ] TCP|UDP|ALL                                          \r\n      [ port = ] 1-65535                                          ...

This shows us that its not taking the "" qoutes.
0
 
Éric MoreauSenior .Net ConsultantCommented:
your syntax is not showing the last parameter. are you sure you can add it?

if you type at dos prompt with that last parameter, does it work?

have you tried to remove the /
0
 
PeterSchenkAuthor Commented:
Works at the dos prompt.... removing the / does nothing.
0
 
Éric MoreauSenior .Net ConsultantCommented:
and what are you EXACTLY typing at dos prompt?
0
 
PeterSchenkAuthor Commented:
netsh.exe firewall add portopening TCP 1122 "PortName"
0
 
PeterSchenkAuthor Commented:
That Worked....

Thank you
0
 
Éric MoreauSenior .Net ConsultantCommented:
from the beginning we were asking what was that / !
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
If you had bothered to take note of my very first comment (and its repeat some time later) this problem would have been resolved some hours ago.  
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.