Calling Command Line Commands from C# – Quick Tip

Hi Folks,

In today’s no code world and AI, while most of the Apps are developed using low code approach, sometimes we have to go with the traditional way of development to handle any integrations with other systems.

When we give anyone Command Line script and ask them to execute, the other person would immediately open Search bar at the bottom available in Windows and start entering cmd. Immediately command prompt window appears and will be able to execute the same command.

But what if we ask to execute command line Commands from C# code…? So, in this blog post, I will show you how easily you can call command line commands with a simple example. Let’s get started…

Here in order to showcase, I will just use a basic command line command and run it from C#.

Everyone knows how to find the ipconfig command right, which just shows the internet protocol configuration when entered in command line like below.

In order to execute it from Console Application using C#, we would need to utilize the System. Diagnostics. You can utilize the below C# code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
namespace BatchTest
{
class Program
{
static void Main(string[] args)
{
Process pro = new Process();
pro.StartInfo.FileName = "cmd.exe";
pro.StartInfo.CreateNoWindow = true;
pro.StartInfo.RedirectStandardInput = true;
pro.StartInfo.RedirectStandardOutput = true;
pro.StartInfo.RedirectStandardError = true;
pro.StartInfo.UseShellExecute = false;
pro.Start();
pro.StandardInput.WriteLine("ipconfig");
pro.StandardInput.Flush();
pro.StandardInput.Close();
pro.WaitForExit();
Console.WriteLine(pro.StandardOutput.ReadToEnd());
Console.ReadKey();
}
}
}

When we execute this command, it shows exactly same as what we saw above with Command Line.

In the same way we can call any Command Line Commands from C#. I have to use this approach for my Power Platform Implementation integration to decrypt encrypted messages using PGP and I found it to be very helpful and thought of sharing with all of you. If you were looking for a program to decrypt, you can check out for previous blog post here.

Cheers,

PMDY

Unable to persist the profile – Quick Tip

Hi Folks,

Are you debugging the Dynamics 365 Plugins using Plugin Profiler, did you ever notice this problem that you were unable to persist profile so as to debug your plugin. Did you got frustrated as you couldn’t capture the profile even after lot of tries installing and uninstalling the profiler. Just read on. I am writing this blog post after fixing a similar situation with one of my Plugin.

First of all, I would advise you to check the below.

  1. Plugin trace log under Settings –> Plugin Trace Log.
  2. Check if your Plugin is being called multiple number of times
  3. Check the filtering attributes of your Plugin whether it is causing to go in an infinite loop
  4. Suppose if you have added an image, did you select the respective attributes of the image
  5. Did you add sufficient depth conditions to prevent infinite loop executions.
  6. At what step is your plugin running, is it in PreOperation, PostOperation.? In case you were throwing an error, change it to Prevalidation step and check.
  7. Were you using persist to entity option while debugging, try changing to throw an error and see.
  8. If you note that the system becomes unresponsive and you were not able to download the log file, then definitely your logic is getting called multiple times. Please reverify.

Once you have verified these, you should be able to find out the exact root cause of the issue…I will leave to yourself.

Thank you…and enjoy debugging…Power Platform Solutions…

Cheers,

PMDY