|
I want to send a set of arguments to a command line with my VC# app. Below is what i have. it will open the command window to the desired directory but i cannot figure out how to pass data to the command line so that i can run a bat file automatically. Can anyone help?
System.Diagnostics.Process Process = new System.Diagnostics.Process();
Process.EnableRaisingEvents = false;
string strCmdLine;
strCmdLine = FindFileLoc.Text;
Process.StartInfo.FileName = "cmd.exe";
Process.StartInfo.WorkingDirectory = strCmdLine;
Process.Start();
Process.WaitForExit();
Process.Close();
|
|
|
|
|
I think you would use the Arguments property which is in Process.StartInfo , like this:
Process.StartInfo.Arguments = strCmdLine Let me know if that does not solve the problem.
Also the WorkingDirectory field is to give the process a starting directory, so do not set it equal to strCmdLine .
Sincerely,
Alexander Wiseman
|
|
|
|
|
In addition to Alexander's answer...
If you are passing a string which contains spaces as a single command line parameter, you need to wrap that string in double quotes("). Otherwise the string is split into multiple parameters, which can cause some rather curious bugs.
For example:
proc.StartInfo.Arguments = "This is four parameters!";
proc.StartInfo.Arguments = "\"This is one parameter!\"";
Josh
|
|
|
|
|
sorry, strcmdline is the starting directory the actual command line arguments come from another field.
-- modified at 16:02 Thursday 15th June, 2006
|
|
|
|
|
No problem, I kind of realized that after I looked at your message again, but I left the note in my message because I thought you might be putting them together into one string (that is, both the started directory and the arguments).
Did the Arguments field work as you wanted it to?
Sincerely,
Alexander Wiseman
|
|
|
|
|
i added the arguments like this
process.StartInfo.Arguments = "argument"
also tried it like this
process.StartInfo.Arguments = "/"argument/""
still does not paste the argument to the command line.
|
|
|
|
|
I think everyone's confused on the terminology. From what I gather, you're opening up a command prompt and you want certain text to appear on the command prompt. This is different than command line arguments. In this case, I think you want to redirect standard input on the command prompt. If so, see my earlier post on this thread.
Logifusion[^]
|
|
|
|
|
I just tried using the Arguments property to call a batch file using the command shell (cmd.exe) and it worked. I think your problem may be in your command line arguments. Do you prefix the path to your batch file with the /C switch? If you look at the help for "cmd.exe" (you can do this by going Start > Run and typing: "cmd.exe /?"), you need the /C switch to execute a command with the shell.
For instance, if I have a batch file called "simple.bat" on the C: drive, then this command (even at the command prompt or the run dialog) will not run the batch file:
cmd.exe "C:\simple.bat"
Rather, you must enter the command this way:
cmd.exe /C "C:\simple.bat"
Thus, your Arguments property should look like this:
process.StartInfo.Arguments = "/C \"C:\\simple.bat\""
If you do not have the /C switch in there, then when you call the process cmd.exe, it will ignore your batch file argument. Let me know if that works.
Sincerely,
Alexander Wiseman
-- modified at 16:25 Thursday 15th June, 2006
[EDIT]
EricDV has posted a much quicker way of opening a batch file in his response below. However, if you positively need to do it with cmd.exe, then I think you'll need the /C switch.
[/EDIT]
|
|
|
|
|
|
|
If your goal is to run a batch file, then you should just set the .FileName to the batch file:
Process.StartInfo.FileName = "mybat.bat";
----------
Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them.
- Laurence J. Peters
|
|
|
|
|
it will not run that way. You cannot double click the bat file you must open a command window and then type the name of the bat you wish to run.
|
|
|
|
|
Sorry, I didn't actually try it. That is odd. This will work though: (I tried it)
Process oProcess = new System.Diagnostics.Process();
oProcess.StartInfo.FileName = "cmd.exe";
oProcess.StartInfo.Arguments = "/C \"MyBat.bat\"";
oProcess.StartInfo.WorkingDirectory = "C:\\";
oProcess.Start();
oProcess.WaitForExit();
-- modified at 17:18 Thursday 15th June, 2006
[EDIT]
Alexander Wiseman beat me to this answer -> see above[^]
[/EDIT]
----------
Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them.
- Laurence J. Peters
|
|
|
|
|
I still can't get the command to run correctly. I have tried it use the process.startinfo.arguments and useing the redirectstandardinput methods and both produce the same results. the cmd.exe just flashes and no changes are made to the file that i am trying to change. i even tried leaving of the /C, when i do this the cmd.exe comes up and stays but does not paste the input to the command line. I am beginning to believe that i am just going to have to stick with the user inputting the data into the command window instead of the program.
<EDIT>
sorry i was leaving off the file extension because that is the way that is done when typed by a user. it works now thanks for all the help.
-- modified at 10:52 Friday 16th June, 2006
|
|
|
|
|
Hi,
In my application I have various SQL stored procedures (In SQL Server 2005) and now I want to add the tracing feature. If turned on, then print some informational lines onto a file stating the control path and if turned off, do not write to the file.
Is this possible by using the cool new feature "CLR Integration". If yes can anybody point me to some links. I have some basic knowledge of C# as well.
Thanks,
|
|
|
|
|
This sounds like a reasonable thing to do with an assembly hosted by SqlServer 2005, but I'm not sure if it's really necessary to jump through extra hoops just for simple logging. Could you not, for example, create a table which stores the log messages (instead of writing them to a file)? That would probably be more performant than calling into an assembly, plus, you could query that table in various ways - something not possible with pure text.
Just a thought...
Josh
|
|
|
|
|
I have this code:
for (int i = 0; i < pnlControls.Controls.Count - 1; i++)
{
pnlControls.Controls.Remove(pnlControls.Controls[i]);
}
but, it doesn't remove the controls.
Can someone tell me how I correctly remove controls from the panel.
Thanks,
Nick
1 line of code equals many bugs. So don't write any!!
|
|
|
|
|
You should do a reversed iteration over the Controls, starting with int i = pnlControls.Controls.Count - 1; . This is necessary because if you remove items from a collection as you iterate over the collection (from start to end), every time an item is removed, the other items shift their index to fill the open space. For example, when you remove item[0], item[1] moves into [0], and item[2] moves into [1], etc.
Josh
|
|
|
|
|
Josh Smith wrote: int i = pnlControls.Controls.Count - 1
Referring to objects in a collection using an index is ugly to me anyways and reminds me of ugly VB code.
But, I found my problem. I had an issue in the container object. To long to explain.
Thanks for helping.
It was just crap code to see its working or not.
Thanks,
Nick
1 line of code equals many bugs. So don't write any!!
|
|
|
|
|
Hello everybody.
Sometimes my application (even in debug mode) fails to continue on the opening command when Im trying to open some COM-port:
SerialPort p = new SerialPort(port);<br />
p.Open();
...
Application do nothing. No CPU time spending, no memory spending...
But sometimes it`s all ok. And there`s no any problems in the SAME methods with the same code.
try-catch blocks not helps in this case.
Problem can be solved by restarting PC or by reconnecting devices connected to COM-ports.
But it is very and very not desirable.
Anybody knows how I can solve this with my code ?
Many Thanks,
Eugene Ostroukhov.
|
|
|
|
|
Hi,
I get errors reading an xml file with my MDA, and its because of this header in the xml file:
<?xml version="1.0" standalone="yes"?>
How can i remove it and make sure that its not comming back?
Manually didn't work...
|
|
|
|
|
Use XmlWriterSettings and set the ConformanceLevel to Fragment . Then, use XmlWriter.Create() to create your writer with the settings.
Logifusion[^]
|
|
|
|
|
Actually, you can also set OmitXmlDeclaration to true. Here's a code snippet to show you how it works with an XmlSerializer (which doesn't let you set the conformance level to Fragment ).
XmlSerializer xs = new XmlSerializer(obj.GetType());
StringBuilder sb = new StringBuilder();
XmlWriterSettings xws = new XmlWriterSettings();
xws.Indent = true;
xws.IndentChars = " ";
xws.OmitXmlDeclaration = true;
XmlWriter xw = XmlWriter.Create(new StringWriter(sb), xws);
xs.Serialize(xw, obj);
return sb.ToString();
Logifusion[^]
|
|
|
|
|
Hi,
What does OmitXmlDeclaration do?
And do i need all that code to use OmitXmlDeclaration?
Because all i want is to prevent that header being added in the xml file.
Thanks in advance!
|
|
|
|
|
OmitXmlDeclaration[^]
The code is an example. You have to be writing to the xml file somehow, so just use XmlWriter and XmlWriterSettings .
Logifusion[^]
|
|
|
|