Category Archives: C#.NET

How to upload files to server using the new FileUpload control in C#/C-Sharp ?

By | June 6, 2012

Hi, For uploading files to server using the new FileUpload control in C#, [c] <%@ Page Language="C#"%> <script runat="server"> protected void Button1_Click(object sender, EventArgs e) { if (FileUpload1.HasFile) try { FileUpload1.SaveAs("C:\\Uploads\\" + FileUpload1.FileName); Label1.Text = "File name: " + FileUpload1.PostedFile.FileName + "<br>" + FileUpload1.PostedFile.ContentLength + " kb<br>" + "Content type: " + FileUpload1.PostedFile.ContentType; } catch (Exception ex) {… Read More »

Uploading Files to Server in C#/C-Sharp

By | June 6, 2012

Hi, For uploading files to server in C#/C Sharp, [c] <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="UploadFile" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <div> <asp:FileUpload ID="Uploader" runat="server" Height="24px" Width="472px" />&nbsp; <asp:Button ID="cmdUpload" runat="server" Height="24px" OnClick="cmdUpload_Click" Text="Upload" Width="88px" /><br /> <br /> <asp:Label ID="lblInfo" runat="server" EnableViewState="False"… Read More »

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 »

By | March 28, 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(&quot;c:yourFile.txt&quot;)); } } [/csharp] It will return either True or False depending on whether your file exists or not. πŸ™‚

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 »

C#/C Sharp – Random Access File Example

By | June 12, 2011

Hi, Given below is an example of a C#/C Sharp Random Access File. [csharp] using System; using System.IO; class MainClass { public static void Main() { FileStream alphaStr; char alphaChr; try { alphaStr = new FileStream("randomDatFile.dat", FileMode.Create); } catch(IOException exc) { Console.WriteLine(exc.Message); return ; } // Write the alphabet. for(int i=0; i < 26; i++) { try {… 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 »

Copy a file in C Sharp/C# without using FileInfo

By | May 16, 2011

Hi, We have discussed here copying a file using FileInfo. Now we are going to see how we can do the same without using FileInfo in C Sharp/C#. [csharp] using System; using System.IO; class MainClass { public static void Main(string[] args) { int i; FileStream fileIn; FileStream fileOut; try { fileIn = new FileStream("opFile.txt", FileMode.Open); } catch(FileNotFoundException exc)… 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. πŸ™‚

How to create a text file in C# ?

By | April 23, 2011

Hi, How can you create a text file in C# and write to it? Sometimes you may need to create a text file using C sharp program. Here is how you can do that. [csharp] using System; using System.IO; class MainClass { static void Main(string[] args) { StreamWriter yourStream= null; string yourString= "I Love Coderz Heaven!"; try {… Read More »

A simple exception handling using C#

By | April 23, 2011

Hi, See how a very simple exception handling works with try-catch methods using C#. [csharp] using System; class MainClass{ public static void Main(){ int divOne = 0; try { int ans = 79 / divOne ; } catch (Exception e) { Console.WriteLine("You got the exception-> " + e.Message); } } } [/csharp] Output- “You got the exception-> Attempted… Read More »

How to find a leap year using C# ?

By | April 18, 2011

Hi, Sometimes you may need to find whether a particular year is leap year or not. But how can you find out whether a particular year is leap year or not in C# ? Use the following code. [csharp] using System; class MainClass { public static void Main() { bool myResult= DateTime.IsLeapYear(2011); Console.WriteLine("Is the year 2011 a leap… Read More »