1

For part of a C# application I am executing some relatively simple networking commands. (ping, ipconfig, tracert, nslookup, etc.)

I have read this answer on when to use C# vs CMD/PowerShell in the general sense: https://stackoverflow.com/a/4188135/8887398

However, I'm wondering particularly for networking, and the Systems.Net library, is there any major advantage in taking the time to write code to implement these in C# as opposed to creating a new command line Process() within a C# app and executing it that way? (The Process() route is really simple/easy to code within C# app)

Question: What are my main advantages of implementing networking commands with the C# Systems.Net library vs creating a new Process() within a C# app and proceed internally as if you were using the command line?

Birdman
  • 1,404
  • 5
  • 22
  • 49
  • In programing we have to pay taxes, aka system overhead. Windows levies the taxes (system overhead) at process creation and at window creation. All subsequent operations on windows processes and windows will be quick. So shelling is always very, very slow. – CatCat Dec 02 '18 at 08:14
  • See for a description of creating a window overhead. https://blogs.msdn.microsoft.com/oldnewthing/20050315-00/?p=36183 – CatCat Dec 04 '18 at 02:18

1 Answers1

0

There is certainly nothing wrong with starting a process. You can even start PowerShell and use that for sequences of actions that are easy to do in PowerShell.

Starting a process is not as easy as you might think.

  1. You need to read both standard output and error for example. Else, the process will hang randomly. Doing this is surprisingly hard.
  2. Getting results from commands is harder compared to using a .NET class. In .NET you get objects and exceptions back. A process can only send you text and an error code.
  3. There is also more overhead. Whether that matters depends on the frequency of such operations.
  4. You could leave child processes orphaned. The best solution is using Windows Job Object to kill the child tree when the parent exits.

So it depends on the specific case. I would definitely do a ping from C# since that is very easy to do. Other commands might benefit more from starting a process.

usr
  • 168,620
  • 35
  • 240
  • 369