Tag Archives: C Sharp

How to Check if File Exists in C Sharp/C# ?

By | April 20, 2012

Hi, Use the following simple lines of code to check whether a particular file exists or not using C#/C Sharp. [csharp] using System; using System.IO; static class MainClass { static void Main(string[] args) { Console.WriteLine(File.Exists("c:yourFile.txt")); } } [/csharp] It will return either True or False depending on whether your file exists or not. πŸ™‚

Program to reverse the digits of a number in C Sharp/C#

By | April 3, 2012

Hi, Use the following code if you want to reverse the digits of a number using C Sharp/C #. [csharp] using System; class MainClass { public static void Main() { int urNum; int urdigits; urNum = 123; Console.WriteLine("Given Number: " + urNum); Console.Write("Reversed Number: "); do { urdigits = urNum % 10; Console.Write(urdigits); urNum = urNum / 10;… Read More »

Sort elements in a String Array – C Sharp / C#

By | June 21, 2011

Hi, In order to sort the elements in a string array in C Sharp/C#, we use the sort() method. Given below is a simple example using sort() method to sort the elements in a string array. [csharp] using System; class MainClass { public static void Main() { string[] urStringArray = {"xylo", "apple", "zeebra", "coderz", "heaven123", "heaven777"}; Array.Sort(urStringArray );… Read More »

A simple Exception Handling example – C Sharp/C#

By | June 18, 2011

Hi, As you know an exception is an error that occurs during the runtime. Here is a simple exception handling example using C Sharp/C#. [csharp] using System; class MainClass{ public static void Main(){ int value= 0; try { int i = 7/value; } catch (DivideByZeroException e) // catching divide by zero exception { Console.WriteLine("DivideByZero {0}", e); } catch… Read More »

Program for Palindrome checking in C Sharp/C#

By | May 17, 2011

Hi, Sometimes you need to check for whether the given string is Palindrome or not. Given below is a sample code to check whether the given string is Palindrome or not using C Sharp/C#. [csharp] class Palindrome { static void Main() { string checkStr, sampleStr; char[] temp; System.Console.Write("Enter your String: "); sampleStr = System.Console.ReadLine(); checkStr = sampleStr.Replace(" ",… Read More »

Copying a file using FileInfo – C Sharp/C#

By | May 16, 2011

Hi, For copying a file using FileInfo in C Sharp/C#, use the following code. For copying a file without using file info check the example given here. [csharp] using System; using System.IO; public class MainClass { static void Main(string[] args) { FileInfo testFile = new FileInfo(@"c:NewWorkstestOld.txt"); testFile.Create(); testFile.CopyTo(@"c:NewProjtestNew.txt"); } } [/csharp] πŸ™‚

C Sharp/C# – Writing to a text file

By | May 7, 2011

Hi, Use the following example code, if you want to write to a text file using C Sharp/C#. [csharp] using System; using System.IO; class MainClass { public static void Main() { FileStream trialStream = new FileStream("sampleText.txt", FileMode.Create); StreamWriter trialWrite = new StreamWriter(trialStream ); trialWrite.WriteLine("{0} {1}", "CoderzHeaven", 100); trialWrite.Close(); trialStream .Close(); } } [/csharp] πŸ™‚

C Sharp/C# – String Uppercase/Lowercase Conversion

By | April 28, 2011

Hi, It will be very useful to know how you can convert a given string into uppercase or lowercase using program. We are going to show you , how to convert a given string into uppercase or lowercase using C Sharp/C#. See the example given below. [csharp] using System; class MainClass { public static void Main() { string… Read More »

Compare two strings in C Sharp/C# – Case Insensitive

By | April 27, 2011

Hi, Sometimes you may need to check whether two strings are equal or not equal by checking case insensitive. See the example below to see how to achieve the same using C Sharp /C#. [csharp] using System; class MainClass { public static void Main() { string strOne = "heaven"; string strTwo = "HEAVEN"; if(String.Compare(strOne , strTwo , true)… Read More »

Program to generate Fibonacci series in C Sharp/C#

By | April 27, 2011

Hi, Here is a simple program to generate Fibonacci series using C Sharp /C#. It will generate Fibonacci numbers < 100. [csharp] class MainClass { public static void Main() { int oldNum = 1; int presentNum = 1; int nextNum; System.Console.Write(presentNum + ","); while (presentNum < 100) { System.Console.Write(presentNum + ","); nextNum = presentNum + oldNum; oldNum =… Read More »

Get files from a directory – C#/C Sharp

By | April 25, 2011

Hi, Sometimes you may need to get the files from a specified directory using C# / C Sharp. See the sample code on how it works. [csharp] using System; using System.IO; class MainClass { public static void Main() { string[] yourFiles = Directory.GetFiles("c:"); foreach (string fNames in yourFiles) Console.WriteLine(fNames); } } [/csharp] This code will get you files… Read More »

C#/C Sharp – Delete file in a folder

By | April 25, 2011

Hi, Let’s see a simple example of deleting a file in a folder using c#. [csharp] using System; using System.IO; public class MainClass { static void Main(string[] args) { FileInfo sampleFile= new FileInfo(@"c:SamplestestFile.txt"); sampleFile.Create(); sampleFile.Delete(); } } [/csharp] sampleFile.Create() will create the file in the specified folder. sampleFile.Delete() will delete the file in the specified folder. πŸ™‚