Thursday, April 21, 2011

shayari

आपसे नहीं बिछड़े ज़िन्दगी से बिछड़े है 
हम चिराग अपनी रौशनी से बिछड़े है 
इस से बढ़ कर क्या होगा ताना मुकद्दर का 
जिस से भी मोहबत की हम उसी से बिछड़े है 

सितमगारो का है फरमान घर भी जाएगा 
अगर में झूट न बोला तो सर भी जायेगा 
बनाईये न किसी के लिए न ताज महल 
हुनर दिखाए तो दस्ते-इ-हुनर भी जायेगा 
माँ चलती है बच्चो के पाँव से जैसे 
उधर ही जाएगी बच्चा जिधर भी जायेगा

C# printing column names of datatable

Ah ..sometimes simple tasks baffle me...Here is a function to print column names of a datatable


private void PrintColumnNames(DataTable dt)
{
    // For each column, print the ColumnName.
        foreach(DataColumn column in dt.Columns)
        {
            Response.Write(column.ColumnName);
        }
}

Tuesday, April 19, 2011

C# file and directory operations

So, I was facing the task for displaying the directory and file contents for each directory and could not find any good help... anyways, here are the functions as I came up with them

Displaying all the directory names along with date last modified


    private string displayDirectories( DirectoryInfo parentDirectory )
    {
        DirectoryInfo[] diArrGE = parentDirectory.GetDirectories();
        // Response.Write(parentDirectory.Name + "

");
        String link = "
";
        foreach (DirectoryInfo dri in diArrGE)
            {
            Int32 index = dri.Name.IndexOf('_');
            String dirName = dri.Name.Substring(index).Replace("_", " ");
            link += string.Format("
", category, dri.Name, dirName, dri.LastWriteTime.ToLongDateString());
            }
        link += "
{2} (Last updated: {3})
";
     
        return link;
        }



Displaying all the files in a directory sorted by date modified... 



    private string displayFiles( DirectoryInfo di )
        {
        string link = string.Empty;
         StringBuilder test = new StringBuilder();
//I just need html files in the directory
        FileInfo[] rgFiles = di.GetFiles("*.html");


        try
            {
            string category = "";
            if (di.FullName.Contains("Agenda"))
                {
                category = "Agenda";

                }
            else if (di.FullName.Contains("Minutes"))
                {
                category = "Minutes";
                }
            else
            {
                category = "Panel";
            }

            // Now read the creation time for each file
            DateTime[] creationTimes = new DateTime[rgFiles.Length];
            for (int i = 0; i < rgFiles.Length; i++)
                creationTimes[i] = rgFiles[i].LastWriteTime;
         //   Response.Write(creationTimes.Length);
            // sort it
            Array.Sort(creationTimes, rgFiles);

            foreach (FileInfo fi in rgFiles)
                {

           
                string[] result = fi.Name.Split(new Char[] { '.' });


                string[] fileName = result[0].Split('_');

                string fileNameDisplay = string.Format("{0} {1},{2}", fileName[1], fileName[0], fileName[2]);

                //  Response.Write(fileNameDisplay);


                link = "" + fileNameDisplay + "   {Last Update: " +
                               fi.LastWriteTime.ToString("D") + "}";

                test.Insert(0, link);
                }
            //link = "";
                test.Insert(0, "
");
            test.Append("
");
            }
        catch (Exception e)
            {

            Response.Write("Error in file " + e);
            }
        return test.ToString();
        }

I really liked the sorting function

       // Now read the creation time for each file
            DateTime[] creationTimes = new DateTime[rgFiles.Length];
            for (int i = 0; i < rgFiles.Length; i++)
                creationTimes[i] = rgFiles[i].LastWriteTime;
         //   Response.Write(creationTimes.Length);
            // sort it
            Array.Sort(creationTimes, rgFiles);


WOO WOO for that