Sponsored Ad

Wednesday, December 29, 2010

How to Download and Install .NET Framework Version 4.0

.NET framework version 4.0 is released and available for free download and install on your machine. Direct link of Microsoft .NET framework 4.0 version is given below. You can go for any of the link given below and the link will redirect you to MSDN website for download.

Please note that you should have windows XP Service pack 3 or higher version to install the .NET framework 4 on your machine. You also need windows installer 3.0 or later version to install this.

Some important features of .NET framework 4.0:

  • CLR and BCL (Improved)
  • ADO.NET (Improved) 
  • ASP.NET Enhancements
  • Windows Presentation Foundation  - WPF (Improved)
  • Windows Workflow Foundation (WWF) and Windows Communication Foundation (WCF) (Improved)

 

http://msdn.microsoft.com/en-us/netframework/aa569263

or

http://www.microsoft.com/downloads/en/details.aspx?FamilyID=9cfb2d51-5ff4-4491-b0e5-b386f32c0992&displaylang=en

Thursday, December 16, 2010

C# File Operations: Delete File Attribute

C# file operations includes the changing of attributes of file also. This example will help you to remove the readonly file attribute from a given file. Example shows that the file have read only attribute and we have removed the readonly attribute by masking using & operator. 

C# File Operations: Delete File Attribute

C# Program:

using System;
using System.IO;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            string txt_file = @"c:\Demo_File.txt";

            File.SetAttributes(txt_file,FileAttributes.ReadOnly);

            bool is_ReadOnly = ((File.GetAttributes(txt_file) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly);
            Console.WriteLine("ReadOnly Attribute After Add: " + is_ReadOnly.ToString()); 

            // delete/clear ReadOnly attribute
            File.SetAttributes(txt_file, File.GetAttributes(txt_file) & ~FileAttributes.ReadOnly);

            is_ReadOnly = ((File.GetAttributes(txt_file) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly);
            Console.WriteLine("ReadOnly Attribute After Remove: " + is_ReadOnly.ToString());           

            Console.ReadLine();
        }    
    }
}

How to Clear/Remove Files Attributes using C#

This is next post of File attributes. This will help you to modify/Clear the file attributes. You need to set file attribute to FileAttributes.Normal, if you want to clear the file attributes. Once you assign normal attribute it will clear all  the attributes assigned to it.

How to Clear/Remove Files Attributes using C#

C# Code to Clear any File Attributes:

using System;
using System.IO;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            string my_file = @"c:\myfile.txt";         

            // set archive and read only attributes
            File.SetAttributes(my_file, FileAttributes.Archive |
                                         FileAttributes.ReadOnly);
            bool ReadOnly_attribute = ((File.GetAttributes(my_file) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly);
            Console.WriteLine("ReadOnly Attribute: " + ReadOnly_attribute.ToString());

            //clear attributes
            File.SetAttributes(my_file, FileAttributes.Normal);

            ReadOnly_attribute = ((File.GetAttributes(my_file) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly);
            Console.WriteLine("ReadOnly Attribute: " + ReadOnly_attribute.ToString());

            bool Archive_attribute = ((File.GetAttributes(my_file) & FileAttributes.Archive) == FileAttributes.Archive);
            Console.WriteLine("Archive Attribute: " + Archive_attribute.ToString());

            Console.ReadLine();
        }

    }
}

How to Set File Attribute using C#

Are you new to input/output operation in C#. This example will help you to set file attributes in C#. There are four file attributes available with file

1. Hidden
2. Archive
3. System
4. ReadOnly

By using following code you can change the attributes of any file.

How to Set File Attribute using C#

C# Code to Change File Attributes:

using System;
using System.IO;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            string file_location = @"c:\myfile.txt";

            //set normal file
            File.SetAttributes(file_location, FileAttributes.Normal);

            bool bReadOnly = ((File.GetAttributes(file_location) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly);
            Console.WriteLine("ReadOnly Attribute: " + bReadOnly.ToString());

            // set archive and read only attributes
            File.SetAttributes(file_location, FileAttributes.Archive |
                                         FileAttributes.ReadOnly);

            bReadOnly = ((File.GetAttributes(file_location) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly);
            Console.WriteLine("ReadOnly Attribute: " + bReadOnly.ToString());

            bool bArchive = ((File.GetAttributes(file_location) & FileAttributes.Archive) == FileAttributes.Archive);
            Console.WriteLine("Archive Attribute: " + bArchive.ToString());

            Console.ReadLine();
        }

    }
}

Wednesday, December 15, 2010

How to Append Attributes to a File using C#

The below C# code will help to append the new attributes to a file without deleting the existing attributes. for example the below code add the hidden attribute to file Demo_File.txt.

How to Append Attributes to a File using C#

Source Code:

using System;
using System.IO;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            string txt_file = @"c:\Demo_File.txt";

            bool is_Hidden = ((File.GetAttributes(txt_file) & FileAttributes.Hidden) == FileAttributes.Hidden);
            Console.WriteLine("Hidden Attribute Before Append: " + is_Hidden.ToString());

            File.SetAttributes(txt_file, File.GetAttributes(txt_file) | FileAttributes.Hidden);

            is_Hidden = ((File.GetAttributes(txt_file) & FileAttributes.Hidden) == FileAttributes.Hidden);
            Console.WriteLine("Hidden Attribute After Append: " + is_Hidden.ToString());           

            Console.ReadLine();
        }    
    }
}

How to Convert Enum to String and String to Enum in C#

This code will help you to convert a given enum to string and string to enum. The corresponding code is given below. Please let us know if you face any difficulty while doing this.

How to Convert Enum to String and String to Enum in C#

 

Source Code:

using System;

namespace MyApp
{
    enum Enum_Number
    {
        one,
        two,
        three,
        four
    }
    class Program
    {
        static void Main(string[] args)
        {
            Enum_Number var_number = Enum_Number.two;
            string str = var_number.ToString();
            Console.WriteLine(str);

            str = "three";
            Enum_Number var_number2 = (Enum_Number)Enum.Parse(typeof(Enum_Number), str);  // Animal.Dog
            str = "THREE";
            Enum_Number var_number3 = (Enum_Number)Enum.Parse(typeof(Enum_Number), str, true); // case insensitive
            Console.WriteLine(var_number2);
            Console.WriteLine(var_number3);

            Console.ReadLine();
        }

    }
}

How to use Nullable Type in C#

This example help you get familiar with nullable types in C#. You can pass the null values in function and it will not raise any error and you can explicitly check if it contains null values.

How to use Nullable Type in C#

Source Code :

using System;
using System.IO;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            NullValueExample(200);
            NullValueExample(null);
            Console.ReadLine();
        }

        static void NullValueExample(int? Number)
        {
            if (Number.HasValue == true)
            {
                Console.WriteLine("Number has value: " + Number);
            }
            else
            {
                Console.WriteLine("Number has null value: null");
            }

            try
            {
                Console.WriteLine("Print value: = {0}", Number.Value);
            }
            catch (InvalidOperationException ex)
            {
                Console.WriteLine("Exception: " + ex.Message);
            }
        }
    }
}

Tuesday, December 14, 2010

How to use Finally Block to Dispose Variables in C#

This example demonstrate that how to use finally block to dispose object at the end of code execution. For example we have open a file "write_file.txt" to  write something but this file do not exist at particular location. now it throw the exception and in finally block we dispose the file stream object.  Instead of using finally block if we use simple dispose statement in same block so in case of exception the object will not be dispose. So to follow the best practice always use finally block to dispose the objects.

How to use Finally Block to Dispose Variables in C#

Code:

using System;
using System.IO;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            FileStream FS_out = null;
            try
            {
                FS_out = File.OpenWrite("write_file.txt");              
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                if (FS_out != null)
                {
                    FS_out.Close();
                    Console.WriteLine("output file stream disposed.");
                }            
            }
            Console.ReadLine();
        }
    }
}

How to use Switch Statement with Enum in C#

This sample code will help you to implement switch statements using the enums. This is good practice as sometimes you can not remember exact switch option and misspelled. So always use enums if want to hardcode some values.

How to use Switch Statement with Enum in C#

Code:

using System;
using System.Collections.Generic;

namespace MyApp
{  
    public enum Number
    {
        one,
        two,
        three
    }
    class Program
    {
        static void Main(string[] args)
        {  
            Number enum_variable = Number.two;

            switch (enum_variable)
            {
                case Number.one:
                    Console.WriteLine("The Selected Number is one.");
                    break;
                case Number.two:
                    Console.WriteLine("The Selected Number is two.");
                    break;
                case Number.three:
                    Console.WriteLine("The Selected Number is three.");
                    break;
            }
            Console.ReadLine();
        }
    }
}

How to use Boxing in C#

Boxing is a method to convert any datatype to object type. All datatypes in C# are derived from base class “object”. So you can directly convert a type to object type. The below code will help you to convert into object type.

class myclass
{
   static void Main()
   {
      int myinteger = 12;
      object obj = myinteger;      // boxing

   }
}

In the above example we are trying to convert int type to object type and it is successfully converted.

 

 
Sponsored Ad

Website Update

Followers