Thursday, April 7, 2016

EnumerateFiles in DirectoryInfo vs GetFiles in c#

When a folder need to be read for the content of files, the GetFiles() method is widely used. But it has some negative impacts as well for loading a folder content that has lot of files. To overcome this issue, EnumerateFiles can be used. Look at the following method,

 private static string GetFilePathFromDirectory(string path, string fileName)  
     {  
       DirectoryInfo di = new DirectoryInfo(path);         
       foreach (FileInfo fi in di.EnumerateFiles(fileName))  
       {  
         if (Path.GetFileNameWithoutExtension(fi.Name).ToLower() == (fileName).ToLower())  
           fi.Name;  
       }  
       foreach (FileInfo fi in di.EnumerateFiles(fileName + ".*"))  //if the extension doesn't match, find another with same name
       {  
         if (Path.GetFileNameWithoutExtension(fi.Name).ToLower() == (fileName).ToLower())  
           fi.Name;  
       }        
       return string.Empty;  
     }  

And see why we shouldn't use GetFiles when it comes to large number of files.

The EnumerateFiles and GetFiles methods differ as follows: When you use EnumerateFiles, you can start enumerating the collection of names before the whole collection is returned; when you use GetFiles, you must wait for the whole array of names to be returned before you can access the array. Therefore, when you are working with many files and directories, EnumerateFiles can be more efficient.

Read it here on MSDN

Happy coding... :)

Wednesday, April 6, 2016

Converting dd/mm/yyyy formatted string to Datetime in C#

A localized string of Date can based to a DateTime object in C# as below;

 DateTime dtBorn = DateTime.ParseExact(dtBorn, "dd/MM/yyyy", CultureInfo.InvariantCulture)  

It's a french format to C# DateTime object.

Happy Coding...

Tuesday, April 5, 2016

Custom file extension in Visual Studio

Sometimes, we use custom file extensions to make our work easy. For example, we may use .prc for stored procedures and .tbl for table scripts. And many more. But when it opens through visual studio, those files are treated as normal text files.

But what if we need to make them look like some known file format, which for example in the above case, the content of the file should be visible as real sql files.

It is easy,

Go to Tools > Options >  Text Editor > File Extensions and add the extensions that matches with relevant type of editing experiences.


So here is how those files look like now,


It just like it was on SQL Server Management Studio with file format of .sql but it really is a .prc file.




GetFileNameWithoutExtension in C#

No matter the extension, what if you need to find whether there is a file matching to a certain name in that particular directory? Look at the below peace of code,

 private static string GetFilePathFromDirectory(string path, string fileName)  
   {  
     DirectoryInfo di = new DirectoryInfo(path);  
     FileInfo[] smFiles = di.GetFiles();  
     foreach (FileInfo fi in di.GetFiles())  
     {  
       if (Path.GetFileNameWithoutExtension(fi.Name).ToLower() == (fileName).ToLower())  
         return fi.FullName;  
     }  
     return string.Empty;  
   }  

You can pass the folder path, and the file name without extension, if there is a matching file, it will return with the extension.

Copy it and try yourself,

Happy Coding...