How to Calculate the Difference in Months Between two Dates C#

This code snippet lets you calculate the difference in months between two dates. If all you want is simply a difference in the months -completely disregarding the date values- then you can use this: public static int MonthDifference(this DateTime lValue, DateTime rValue) { return (lValue.Month – rValue.Month) + 12 * (lValue.Year – rValue.Year); } Note […]

Read More

How to Hide and Show the Console Window Associated with your own C# app?

Here’s how: Add a reference to InteropServices in your class. using System.Runtime.InteropServices; Next, define two function signatures GetConsoleWindow and ShowWindow. These functions use kernel32.dll and user32.dll respectively. [DllImport(“kernel32.dll”)] static extern IntPtr GetConsoleWindow(); [DllImport(“user32.dll”)] static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); const int SW_HIDE = 0; const int SW_SHOW = 5; Finally, use these functions as […]

Read More

How to get the Month Name in C#?

You can use the CultureInfo to get the month name. Another way is to use extension methods. Here is an example of how to do it using extension methods: Add a DateTimeExtensions class to your project. static class DateTimeExtensions { public static string ToMonthName(this DateTime dateTime) { return CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(dateTime.Month); } public static string ToShortMonthName(this DateTime […]

Read More

How to get thumbnail of Video Storage File in Windows phone app

StorageFile class provides us with the method called GetThumbnailAsync with the help of which we can retrieve the thumbnail of a storage file in Windows phone app.   Important Note: This method works only on the Storage Files that are stored in public directory of the phone. It doesn’t work on the files that are […]

Read More

How to copy one StorageFile into another StorageFile in Windows phone app using C#

StorageFile class provides a method called CopyAsync to copy the contents of one file into another. The code for the above is as following     Note: In the given code above we replace the contents of filetoReplace by filetoCopy.   If you want to copy the file to a given StorageFolder you can use the […]

Read More

How to get the file size of Storage File in Windows phone app

StorageFile class provides different methods with the help of which we can retrieve the file size of a give StorageFile.   In the given function below we retrieve the file size of a given StorageFile using GetBasicPropertiesAsync() method. This method returns the basic properties of the given Storagefile in form of class object from which […]

Read More

Retrieve File Size From Remote URL using C#

The system API class System.Net.HttpWebRequest allows you to retrieve the file information programmatically. The size of each file/page generally available in the header section of web response, which you can query to get it. Header contains Content-Length property value which gives you the size of the file in bytes. This you can later calculate to […]

Read More