Windows Phone Developers

Tuesday, June 21, 2011

How to Change Permission of Folder in Visual Basic / C#

VB.NET Set Folder Permission / C# Set Folder Permission


In this snippet we create a new directory and set the permission of it to Fullcontrol using ACL.

private static void ChangePermissionOfDir()
        {
            //Folder
            DirectoryInfo NewDir = Directory.CreateDirectory(@"C:\Temp\Te21");

            DirectorySecurity dSecur = NewDir.GetAccessControl();

            FileSystemAccessRule fAccess = new FileSystemAccessRule("Everyone",FileSystemRights.FullControl,AccessControlType.Allow  );


            dSecur.AddAccessRule(fAccess);

            NewDir.SetAccessControl(dSecur);
 
    
        }
Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

How to Convert a String to Array of Characters

The following snippet converts a string to a Character Array using CharArray

private static void String2CharArray()        {
            string s1 = "This is a string";
            Char[] ArrayofChars;
            ArrayofChars = s1.ToCharArray();
            Console.WriteLine(s1);
            foreach (char c1 in ArrayofChars)
                Console.WriteLine(c1);
        }
Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

Thursday, June 9, 2011

Queue in C# / Add and Remove items from Queue in C# (.NET)

Queues are common in real world - Flight checkins to School. Here is the way you can use it in C#

private static void CustomerServiceQueue()
        {
            Queue CSQ = new Queue();

            CSQ.Enqueue("Gautam");
            CSQ.Enqueue("Nargis");
            CSQ.Enqueue("Beth");
            CSQ.Enqueue("Slobodan");

            Console.WriteLine("Peeking Queue: {0}", CSQ.Peek());

            Console.WriteLine("Check if CSQ Contains Beth : {0}", CSQ.Contains("Beth"));

            while (CSQ.Count > 0)
            {
                Console.WriteLine(CSQ.Dequeue());
            }
           

        }

Contains checks for presence of the given object in the queue. Peek returns the first element from the queue

Dequeuue does the same as Peek, however, it also removes the object Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

Tuesday, June 7, 2011

How to Override ToString Method in C# (VB.NET)

Customize ToString Method in C# (VB.NET)

Overriding ToString Method has its own advantages.

For example, the following class has four fields

public class ClassExec
    {
        string _Name;
        
        public string Name
        {
            get { return _Name; }
            set { _Name = value ;}
        }

        public string Source
        {
            get;
            set;
        }

        public string Destination
        { get; set; }

        public DateTime  DOT
        {
            get;
            set;
        }

If you try to use the ClassExec.ToString() method it will return the fully qualified Class name as default. This doesn't help us much.

Instead let us override the ToString method to return the four fields in a formatted way

public override string ToString()
        {
            return (Name + ' ' + Source + ' ' + Destination + ' ' + DOT);
        }

The following code will now produce the desired output:

ClassExec cls = new ClassExec();
            cls.Name = "Jackob Johnson";
            cls.Source = "Socchi";
            cls.Destination = "Moscow";
            cls.DOT = DateTime.Parse("12-Jan-2012");

            Console.WriteLine(cls.ToString());

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

System.Xml.Serialization.XmlSerializer.XmlSerializer() is inaccessible due to its protection level

One reason for this error is if you haven't mentioned the type when you initialize the XmlSerializer object

XmlSerializer Xser = new XmlSerializer()

can be replaced with

XmlSerializer Xser = new XmlSerializer(this.GetType() );
Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

Sunday, June 5, 2011

How to CreateFolder in C# (.NET) / How to Create a Directory in C# (VB.NET)

How to check if a directory exists in C# / How to check if a folder exists in C#

Many times backups are created in a new folder with name as date etc. The following snippet checks if directory exists in the particular path and creates it

private static void CreateFolder()
        {
        //This example creates a new folder under MyDocuments

            string folderName = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

            string userName = Environment.UserName;

            folderName = Path.Combine(folderName, userName);

            if (Directory.Exists(folderName) == false)
            {
                Directory.CreateDirectory(folderName);
            }


        }

How to combine two paths in .NET

The example combines two paths using Path.Combine method.

See also:

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon