"No one is harder on a talented person than the person themselves" - Linda Wilkinson ; "Trust your guts and don't follow the herd" ; "Validate direction not destination" ;

February 09, 2012

Tool Developer Notes - Part III

[Previous Post in Series - Tool Developer Notes - Part II]
[Next Post in Series - Tool Developer Notes - Part IV]

Adding few more interesting notes based on learning's

Tip #1 - Working with DateTime Picker in C#. Requirement was to write in specific format. Provided below is an example using WinForms Application

For the Submit button try out the sample code for Setting date, Adding Duration

        private void btnSubmit_Click(object sender, EventArgs e)
            int duration = 0;
            DateTime CurrentTime;
            String CurDate;
            duration = Convert.ToInt32(txtDuration.Text);
            CurrentTime = dTPTimeSet.Value;
            for (int i = 0; i < 10; i++)
                CurDate = CurrentTime.ToString("MM/dd/yyyy hh:mm");
                CurrentTime = CurrentTime.AddMinutes(duration);

Tip #2 - Check if string is GUID. Tweaking the code from Stackoverflow question

public static bool IsGuid(string guid)
            if (!string.IsNullOrEmpty(guid))
                Regex regex = new Regex(@"^(\{{0,1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}\}{0,1})$");
                return regex.IsMatch(guid);
            return false;

Tip #3 - Loop through all files in a directory
How to loop through all files in a folder using C#

Tip #4 - Last File Created time in a directory
using System.IO.DirectoryInfo we find last file created time in a directory

string Folder = @”C:\Test”;
DirectoryInfo dinfo = new DirectoryInfo(Folder).GetDirectories("*", SearchOption.AllDirectories).OrderByDescending(d => d.LastWriteTimeUtc).First();
//Find the Last File Created Time in Directory
DateTime LastCreatedTime = dinfo.LastAccessTime;

Happy Learning!!!

No comments: