Evaluate an mathematical expression

public class MathEvaluator{ public static void Run() { Eval(“(1+2)”); Eval(“5*4/2”); Eval(“((3+5)-6)”); } public static void Eval(string input) { var ans = Evaluate(input); Console.WriteLine(input + ” = ” + ans); } public static double Evaluate(String input) { String expr = input.Substring(0,1) == “(” ? input : “(” + input + “)”; Stack<String> ops = new Stack<String>(); Stack<Double> vals = new Stack<Double>(); for (int i = 0; i < expr.Length; i++) { String s = expr.Substring(i, 1); if (s.Equals(“(“)){} else if (s.Equals(“+”)) ops.Push(s); else if (s.Equals(“-“)) ops.Push(s); else if (s.Equals(“*”)) ops.Push(s); else…

Read More

Using HTTP Methods for RESTful Services

The HTTP verbs comprise a major portion of our “uniform interface” constraint and provide us the action counterpart to the noun-based resource. The primary or most-commonly-used HTTP verbs (or methods, as they are properly called) are POST, GET, PUT, and DELETE. These correspond to create, read, update, and delete (or CRUD) operations, respectively. There are a number of other verbs, too, but are utilized less frequently. Of those less-frequent methods, OPTIONS and HEAD are used more often than others. Below is a table summarizing recommended return values of the primary…

Read More

To PUT or POST?

Create, Update and HTTP Idempotence For developers building REST-based APIs, there is a great deal of misinformation and some understandable confusion about when to use HTTP PUT and when to use HTTP POST. Some say, POST should be used to create a resource, and PUT to modify one. Others that PUT should be used to create, and POST to modify one. Neither is quite right. Often, developers think of each HTTP method as a 1:1 relationship with CRUD operations: CRUD       HTTPCreate      POSTRead         GETUpdate     PUTDelete      DELETE This can be true, with…

Read More

Setup FileZilla Server Passive Ports on Windows Server 2012

I prefer to use FileZilla FTP Server above the traditional IIS FTP module. FileZilla comes with a lite and lean GUI, great logging tools, connection (speed)limits and more. If you do not know FileZilla Server or the Filezilla Client I strongly encourage you to try them out at https://filezilla-project.org/ Setting up the FileZilla Server is straightforward, but after configuration the user/groups and directory you can have some trouble setting up the Windows Server 2012 Firewall. Traditional FTP uses port 21, you should open this on your Firewall (see below) but…

Read More

C# Tutorial for log4net

Here’s a short tutorial on how to use log4net in C# 1. Get log4net from the apache website or use NuGet to include in your project it   2. Add the Appender section to your app.config. The following code uses a file for the logging: <configuration> <configSections> <section name=”log4net” type=”log4net.Config.Log4NetConfigurationSectionHandler,Log4net”/> </configSections> <log4net> <root> <level value=”DEBUG” /> <appender-ref ref=”LogFileAppender” /> </root> <appender name=”LogFileAppender” type=”log4net.Appender.RollingFileAppender” > <param name=”File” value=”log-file.txt” /> <param name=”AppendToFile” value=”true” /> <rollingStyle value=”Size” /> <maxSizeRollBackups value=”10″ /> <maximumFileSize value=”10MB” /> <staticLogFileName value=”true” /> <layout type=”log4net.Layout.PatternLayout”> <param name=”ConversionPattern” value=”%date [%thread]…

Read More

C# Code Snippet – Download Image from URL

This .Net C# code snippet download image from URL. To use this function simply provide the URL of the image you like to download. This function read the image contents using URL and returns downloaded image as an image object. This function download image using web response stream. /// <summary>/// Function to download Image from website/// </summary>/// <param name=”_URL”>URL address to download image</param>/// <returns>Image</returns>public Image DownloadImage(string _URL){ Image _tmpImage = null; try { // Open a connection System.Net.HttpWebRequest _HttpWebRequest = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(_URL); _HttpWebRequest.AllowWriteStreamBuffering = true; // You can also specify additional…

Read More

Check Local IP Address

This example shows how to detect whether a host name or IP address belongs to local computer. Get local computer name Get local computer host name using static method Dns.GetHostName. [C#] string localComputerName = Dns.GetHostName(); Get local IP address list Get list of computer IP addresses using static method Dns.GetHostAd­dresses. To get list of local IP addresses pass local computer name as a parameter to the method. [C#] IPAddress[] localIPs = Dns.GetHostAddresses(Dns.GetHostName()); Check whether an IP address is local The following method checks if a given host name or IP…

Read More

Download Files from Web

This example shows how to download files from any website to local disk. The simply way how to download file is to use WebClient class and its method DownloadFile. This method has two parameters, first is the url of the file you want to download and the second parameter is path to local disk to which you want to save the file. Download File Synchronously The following code shows how to download file synchronously. This method blocks the main thread until the file is downloaded or an error occur (in…

Read More