Thursday, 11 March 2010

DateTime Manipulation

I was looking for a resource which would explain all the possible options for formatting strings. I cam across http://www.blackwasp.co.uk/CSharpDateManipulation.aspx


DateTime theDate = DateTime.Parse("3 Jan 2007 21:25:30");
string result;

result = theDate.ToString("d-MM-yy"); // result = "3-01-07"
result = theDate.ToString("HH:mm"); // result = "21:25"
result = theDate.ToString("h:mm tt"); // result = "9:25 PM"


It's not an exhaustive list, but it solved my problem.

Wednesday, 6 January 2010

How to get the return value from an invoked application.

On the Windows operating system, an application’s return value is stored within a system environment variable named %ERRORLEVEL%. If you were to create an application that programmatically launches another executable, you can obtain the value of %ERRORLEVEL% using the static System.Diagnostics.Process.ExitCode property.


For example, the following program would return a value of "-1" when invoked:-

static int Main(string[] args)
{
// Display a message and wait for Enter key to be pressed.
Console.WriteLine("***** My First C# App *****");
Console.WriteLine("Hello World! ");
Console.WriteLine();
Console.ReadLine() ;
// Return an arbitrary error code.
return -1;
}



This program could then be called, and the return value evaluated, by using this following batch file for example:-

@echo off
rem A batch file for SimpleCSharpApp. exe
rem which captures the app' s return value.
SimpleCSharpApp
@if "%ERRORLEVEL%" == "0" goto success
: fail
echo This application has failed!
echo return value = %ERRORLEVEL%
goto end
: success
echo This application has succeeded!
echo return value = %ERRORLEVEL%
goto end
: end
echo All Done.

Friday, 29 May 2009

Tip - How to create a GUID


using System.IO;

System.Guid.NewGuid().ToString()
// e.g. a12f98d0-4b36-4edc-99b1-83b6b871ab2c
System.Guid.NewGuid().ToString("N")
// e.g. bf4c57bccef54065ae85c88cc9bfd341
System.Guid.NewGuid().ToString("D")
// e.g. 544d1d5e-0cbb-4c4d-8def-de4f684b0d5d
System.Guid.NewGuid().ToString("B")
// e.g. {6b1ac215-4671-444b-9be2-04126c76e74d}
System.Guid.NewGuid().ToString("P")
// e.g.(db40c0e9-eca2-4123-aeae-c89847e48bba)

Thursday, 28 May 2009

Tip - Get Files from Directory


From - csharp-examples.net



The method Directory.GetFiles returns string array with files names (full paths). For example:-





using System.IO;

string[] filePaths = Directory.GetFiles(@"c:\MyDir\");
// returns:
// "c:\MyDir\my-car.BMP"
// "c:\MyDir\my-house.jpg"





Get files from directory (with specified extension)

You can specify a search pattern. You can use wildcard specifiers in the search pattern, e.g. „*.bmp“ to select files with the extension or „a*“ to select files beginning with letter „a“.





using System.IO;

string[] filePaths = Directory.GetFiles(@"c:\MyDir\", "*.bmp");
// returns:
// "c:\MyDir\my-car.BMP"





Get files from directory (including all subdirectories)

If you want to search also in subfolders use parameter SearchOption.AllDirectories.





using System.IO;

string[] filePaths = Directory.GetFiles(@"c:\MyDir\", "*.bmp",
SearchOption.AllDirectories);
// returns:
// "c:\MyDir\my-car.BMP"
// "c:\MyDir\Friends\james.BMP"