How to execute a dos commend in C#?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

How to execute a dos commend in C#?
Many thanks for your replying.

private void button1_Click(object sender, EventArgs e)
{
//code here please
}
 
vincent said:
How to execute a dos commend in C#?
Many thanks for your replying.

The easiest way is with PowerShell:

private void button1_Click(object sender, EventArgs e)
{
using (RunspaceInvoke ri = new RunspaceInvoke())
{
Collection<PSObject> results =
ri.Invoke("the-command-you-want-to-execute");
}
}
 
Wow! It is beautiful.
Thank you very much.

John Vottero said:
The easiest way is with PowerShell:

private void button1_Click(object sender, EventArgs e)
{
using (RunspaceInvoke ri = new RunspaceInvoke())
{
Collection<PSObject> results =
ri.Invoke("the-command-you-want-to-execute");
}
}
 
It does not work.
I get four error message.
Do you know how to solve it?

Error 1 The type or namespace name 'RunspaceInvoke' could not be found (are
you missing a using directive or an assembly reference?) C:\Documents and
Settings\Administrator\My Documents\Visual Studio
2005\Projects\WindowsApplication9\WindowsApplication9\Form1.cs 21 20 WindowsApplication9
Error 2 The type or namespace name 'RunspaceInvoke' could not be found (are
you missing a using directive or an assembly reference?) C:\Documents and
Settings\Administrator\My Documents\Visual Studio
2005\Projects\WindowsApplication9\WindowsApplication9\Form1.cs 21 44 WindowsApplication9
Error 3 The type or namespace name 'PSObject' could not be found (are you
missing a using directive or an assembly reference?) C:\Documents and
Settings\Administrator\My Documents\Visual Studio
2005\Projects\WindowsApplication9\WindowsApplication9\Form1.cs 23 28 WindowsApplication9
Error 4 The type or namespace name 'Collection' could not be found (are you
missing a using directive or an assembly reference?) C:\Documents and
Settings\Administrator\My Documents\Visual Studio
2005\Projects\WindowsApplication9\WindowsApplication9\Form1.cs 23 17 WindowsApplication9
 
I already install PowerShell, but I do not know how to add a reference to
System.Management.Automation.dll.
Please tell me how to do that, thank you very much.
 
vincent said:
I already install PowerShell, but I do not know how to add a reference to
System.Management.Automation.dll.
Please tell me how to do that, thank you very much.

In Visual Studio, right click "References" and pick "Add Reference".

Click the "Browse" tab.

Navigate to C:\Program Files\Reference
Assemblies\Microsoft\WindowsPowerShell\V1.0\

Pick System.Management.Automation.dll
 
I can't find System.Management.Automation.dll
How do solve this problem.
Many thanks for your erplying.
 
I don't know how you can have PowerShell installed but not have that
assembly. Are you sure that PowerShell is installed? Are there other files
in:

C:\Program Files\Reference Assemblies\Microsoft\Windows\PowerShell\V1.0\
 
I find my PowerShell at "C:\WINDOWS\system32\windowspowershell\v1.0"
But I still can't find "System.Management.Automation.dll"
My OS is "windows server 2003"
 
using (RunspaceInvoke ri = new RunspaceInvoke())
{
Collection<PSObject> results = ri.Invoke("\"C:\\Program
Files\\PDF2allTest\\pdf2all\" -c pdf2jpg -s \"c:\\test\\test\\01.pdf\" -or
600");
}

When I try to execute the codes above, then I get the following error
message. How to solve this?

System.Management.Automation.ParseException was unhandled
Message="You must provide a value expression on the right-hand side of the
'-' operator."
Source="System.Management.Automation"
StackTrace:
at System.Management.Automation.Parser.addExpressionRule(Tokenizer tok)
at
System.Management.Automation.Parser.comparisonExpressionRule(Tokenizer tok)
at
System.Management.Automation.Parser.bitwiseExpressionRule(Tokenizer tok)
at
System.Management.Automation.Parser.logicalExpressionRule(Tokenizer tok)
at
System.Management.Automation.Parser.assignmentStatementRule(Tokenizer tok)
at System.Management.Automation.Parser.pipelineRule(Tokenizer tok)
at System.Management.Automation.Parser.statementRule(Tokenizer tok)
at System.Management.Automation.Parser.statementListRule(Tokenizer tok)
at System.Management.Automation.Parser.Parse(String input, Int32
cursorPosition)
at System.Management.Automation.AutomationEngine.Parse(String script,
Boolean interactiveCommand)
at
System.Management.Automation.ShellFunctionCommandProcessor..ctor(String
script, ExecutionContext context, Boolean isFilter, Boolean useLocalScope,
Boolean interactiveCommand)
at
System.Management.Automation.Runspaces.Command.CreateCommandProcessor(ExecutionContext
executionContext, CommandFactory commandFactory, Boolean addToHistory)
at
System.Management.Automation.Runspaces.LocalPipeline.CreatePipelineProcessor()
at System.Management.Automation.Runspaces.LocalPipeline.InvokeHelper()
at
System.Management.Automation.Runspaces.LocalPipeline.InvokeThreadProc()
 
That doesn't look like a valid command to me (for PowerShell or CMD.EXE). I
suggest opening a PowerShell command window and working out what the command
should be.
 
Thank you very much.
I will take your advise.

John Vottero said:
That doesn't look like a valid command to me (for PowerShell or CMD.EXE). I
suggest opening a PowerShell command window and working out what the command
should be.
 
Back
Top