Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot Access with an Instance Reference

While I can do this:

System.Diagnostics.Process.Start(@"C:\MyFolder\MyProgram.cmd");

I can't do this:

var process = new System.Diagnostics.Process();
process.Start(@"C:\MyFolder\MyProgram.cmd");

Error: Member 'System.Diagnostics.Process.Start(string)' cannot be accessed with an instance reference; qualify it with a type name instead.

What is the reason behind this? Can anyone please explain?

Thanks in advance!

like image 272
JackTheRipper Avatar asked Oct 27 '25 12:10

JackTheRipper


1 Answers

All the parametered overloads of Process.Start are static. If you want to use the second syntax then you have to set instance state first which is just the "filename" property of StartInfo:

var proc = new Process();
proc.StartInfo.FileName = @"C:\MyFolder\MyProgram.cmd";
proc.Start();

Note that this should be equivalent to System.Diagnostics.Process.Start(@"C:\MyFolder\MyProgram.cmd"); because as the MSDN says: "The overload is an alternative to the explicit steps of creating a new Process instance, setting the FileName member of the StartInfo property, and calling Start for the Process instance."

like image 82
Quantic Avatar answered Oct 29 '25 01:10

Quantic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!