Link to home
Start Free TrialLog in
Avatar of PeterSchenk
PeterSchenk

asked on

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();
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

you can do something like this:
string Arguments = "firewall add portopening TCP " + tb3 + "/" + Convert.ToChar(34) + tb5 + Convert.ToChar(34);
Avatar of PeterSchenk
PeterSchenk

ASKER

Same issue did not work
put a breakpoint on the line p.Start(); and inspect what the StartInfo.Arguments actually are.
can you show us your code ?

why do you have a / in your string concatenation?
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”

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();
        }
What was the value of p.StartInfo.Arguments ?
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);
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?
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.
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 /
Works at the dos prompt.... removing the / does nothing.
and what are you EXACTLY typing at dos prompt?
netsh.exe firewall add portopening TCP 1122 "PortName"
ASKER CERTIFIED SOLUTION
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That Worked....

Thank you
from the beginning we were asking what was that / !
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.