cURL Project Application in C#

Over time, and out of necessity, I have been throwing together a few different tools over time using C# to help me cut down the amount of time it takes me to do certain things. As I create those I make them code generic so as to not include anything company wise and I like to share those projects so that others learning to code or who might be searching for code or projects might find them beneficial.

Testing in a QA/eCommerce environment, I use cURL pretty much daily for clearing Varnish cache when testing web pages across different staging servers and was curious about creating a wrapper for the curl.exe in C# that I could include within my standalone portable tool set. I did some research and although I did find several great resources I chose to stick it out with a Microsoft article I found titled How To Write a Wrapper for a Command-Line Tool with Visual C# .NET. The article gives a great explanation of creating a class file and adding it to a project.

The cURL Project Application

My cURL project simply needs to fulfill the function of issuing a purge command at whatever URL I give it so its geared with this specific task in mind. Normally I would stick the folder to the curl.exe file in my system PATH environment variable, open a command prompt and issue curl -X PURGE v1.cms.servername.com but the task, for me, is to include this within my “forms” tools that I created in c#.

Using CMD

Issuing the curl -X PURGE command in the command console would yield the results below. I just need the same thing in my tool which I can do using return “\t” + output in the class file start.cs.

 

curl_cmd

Using cURL Tool

The URL field is for the URL on the server I want to clear varnish cache for. The drop down boxes, respectively, retain the switch and command that I want to issue. The PURGE button simply runs the app and on success will give me a message dialog as shown in the second cURL graphic below. the drop down boxes actually have the switch and command respectively listed three times via loading an array in the form_load event. It’s because I may add a couple more items. If not then I will break these out of an array but still load them in the event.

Code follows as well as a ZIP file to download that contains the project and executable. Enjoy.

mycurlapp

 

curl_app01

Start.cs file based on the Microsoft article

The class can really just get by with using System.Diagnostics and using System.IO. Modify as you see fit for your project.

[code language=”csharp”]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.IO;

namespace MyCurlApp
{
public class start
{
internal static string Run(string exeName, string argsLine, int timeoutSeconds)
{
StreamReader outputStream = StreamReader.Null;
string output = "";
bool success = false;

try
{
Process newProcess = new Process();
newProcess.StartInfo.FileName = exeName;
newProcess.StartInfo.Arguments = argsLine;
newProcess.StartInfo.UseShellExecute = false;
newProcess.StartInfo.CreateNoWindow = true;
newProcess.StartInfo.RedirectStandardOutput = true;
newProcess.Start();

if (0 == timeoutSeconds)
{
outputStream = newProcess.StandardOutput;
output = outputStream.ReadToEnd();
newProcess.WaitForExit();
}
else
{
success = newProcess.WaitForExit(timeoutSeconds * 1000);

if (success)
{
outputStream = newProcess.StandardOutput;
output = outputStream.ReadToEnd();
}
else
{
output = "Timed out at " + timeoutSeconds + " seconds waiting for " + exeName + " to exit.";
}
}

}
catch (Exception e)
{
throw (new Exception("An error occurred running " + exeName + ".", e));
}
finally
{
outputStream.Close();
}
return "\t" + output;

}
}
}
[/code]

Main Form1.cs

[code language=”csharp”]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;

namespace MyCurlApp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
string[] myList = new string[3];
myList[0] = "-X";
myList[1] = "-X";
myList[2] = "-X";
comboBox1.Items.AddRange(myList);
comboBox1.SelectedIndex = 0;

string[] myList2 = new string[3];
myList2[0] = "PURGE";
myList2[1] = "PURGE";
myList2[2] = "PURGE";
comboBox2.Items.AddRange(myList2);
comboBox2.SelectedIndex = 0;

}

private void button1_Click(object sender, EventArgs e)
{
try
{

string output;
string arg1 = comboBox1.Text;
string arg2 = comboBox2.Text;
string arg3 = textBox1.Text;

// My draconian error control. maybe use a switch case if ever using another paramter
// than -X. I only purge with this.

if (textBox1.Text == "" || comboBox1.SelectedItem.ToString() == null || comboBox2.SelectedItem.ToString() == null)
{
//MB works good if textBox is empty
MessageBox.Show("Select a valid parameter or URL!");
return;
}
else
{
// run if all is cool
output = start.Run("Curl.exe", " " + arg1 + " " + arg2 + " " + textBox1.Text, 10);
MessageBox.Show(output + "If blank, check the URL");
}

}
//If fields are blank otherwise show any exceptions
//Should always include basic try and catch in case an error occurs
catch (NullReferenceException ex)
{
MessageBox.Show("\nPerhaps you forgot to select something?\n" + ex.Message);
}

catch (Exception ex)
{
MessageBox.Show("Well this isn’t good! " + "\r\n" + ex.Message);
}

}

private void button2_Click(object sender, EventArgs e)
{
string getcb = Clipboard.GetText(TextDataFormat.UnicodeText);
textBox1.Text = getcb;
}

}
}
[/code]

REFERENCES

Microsoft – How To Write a Wrapper for a Command-Line Tool with Visual C# .NET

DOWNLOAD

Tagged on: , , ,