Friday, February 25, 2011

Compare File versions using System.IO.Version class


Following is the sample code to compare versions for two file:

using System;


using System.Collections.Generic;
using System.Text;
using System.IO;
static void Main(string[] args)
{
           string strFileVersion1 = "1.0.2254.37578";
           string strFileVersion2 = "1.0.2254.36884";
            

           Version version1= new Version(strFileVersion1 );
           Version version2 = new Version(strFileVersion2);

            Console.WriteLine("File Version File 1 = " + strFileVersion1 + " File Version File 2 = " + strFileVersion2);
            

           if (version1 > version2)
                Console.WriteLine(strFileVersion1 + " is Newer then " + strFileVersion2);
           else if (version1 < version2)
                Console.WriteLine(strFileVersion1 + " is Older then " + strFileVersion2);
           else
                Console.WriteLine(strFileVersion1 + " is Equal to " + strFileVersion2);
            Console.ReadLine();
}



OUTPUT:

File Version File 1 = 1.0.2254.37578  File Version File 2 = 1.0.2254.36884
1.0.2254.37578 is Newer then 1.0.2254.36884

File Version File 1 = 1.0.2254.37578  File Version File 2 = 1.0.2254.37578
1.0.2254.37578 is Equal to 1.0.2254.37578

File Version File 1 = 1.0.2154.37578  File Version File 2 = 1.0.2254.37578
1.0.2154.37578 is Older then 1.0.2254.37578

File Version File 1 = 3.0.2154.37578  File Version File 2 = 2.0.2254.37578
3.0.2154.37578 is Newer then 2.0.2254.37578









Tuesday, February 15, 2011

Environment.SpecialFolder Enumeration


In Dot net path for these special folders can be accesses by using the following method:


Environment.GetFolderPath(Environment.SpecialFolder.Personal)
Or
Environment.GetFolderPath(Environment.SpecialFolder.Windows) 

Following are the paths for the  dot net framework versions 2.0,3.5 and 4.0  respectively:


Environment.SpecialFolder (For Dotnet 2.0)

ApplicationData
C:\Users\UserName\AppData\Roaming

CommonApplicationData
C:\ProgramData

CommonProgramFiles
C:\Program Files\Common Files

Cookies
C:\Users\UserName\AppData\Roaming\Microsoft\Windows\Cookies

Desktop
C:\Users\UserName\Desktop

DesktopDirectory
C:\Users\UserName\Desktop

Favorites
C:\Users\UserName\Favorites

History
C:\Users\UserName\AppData\Local\Microsoft\Windows\History

InternetCache
C:\Users\UserName\AppData\Local\Microsoft\Windows\Temporary Internet Files

LocalApplicationData
C:\Users\UserName\AppData\Local

MyDocuments 
C:\Users\UserName\Documents

MyMusic 
C:\Users\UserName\Music

MyPictures 
C:\Users\UserName\Pictures

Personal 
C:\Users\UserName\Documents

ProgramFiles 
C:\Program Files

Programs 
C:\Users\UserName\AppData\Roaming\Microsoft\Windows\Start Menu\Programs

Recent
C:\Users\UserName\AppData\Roaming\Microsoft\Windows\Recent

SendTo 
C:\Users\UserName\AppData\Roaming\Microsoft\Windows\SendTo

StartMenu
C:\Users\UserName\AppData\Roaming\Microsoft\Windows\Start Menu

Startup 
C:\Users\UserName\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup

System 
C:\Windows\system32

Templates 
C:\Users\UserName\AppData\Roaming\Microsoft\Windows\Templates

Environment.SpecialFolder (For Dotnet 3.5)


LocalApplicationData
C:\Users\UserName\AppData\Local

Environment.SpecialFolder (For Dotnet 4.0)

Personal
C:\Users\UserName\Documents
AdminTools
C:\Users\UserName\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Administrative Tools

CDBurning  
C:\Users\UserName\AppData\Local\Microsoft\Windows\Burn\Burn

CommonAdminTools  
C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Administrative Tools

CommonDocuments
C:\Users\Public\Documents

CommonMusic  
C:\Users\Public\Music

CommonPictures  
C:\Users\Public\Pictures

CommonStartMenu  
C:\ProgramData\Microsoft\Windows\Start Menu

CommonPrograms  
C:\ProgramData\Microsoft\Windows\Start Menu\Programs

CommonStartup
C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup

CommonDesktopDirectory
C:\Users\Public\Desktop

CommonTemplates  
C:\ProgramData\Microsoft\Windows\Templates

CommonVideos  
C:\Users\Public\Videos

Fonts  
C:\Windows\Fonts

MyVideos  
C:\Users\UserName\Videos

NetworkShortcuts  
C:\Users\UserName\AppData\Roaming\Microsoft\Windows\Network Shortcuts

PrinterShortcuts  
C:\Users\UserName\AppData\Roaming\Microsoft\Windows\Printer Shortcuts

UserProfile  
C:\Users\UserName

CommonProgramFilesX86  
C:\Program Files\Common Files

ProgramFilesX86  
C:\Program Files

Resources  
C:\Windows\resources

SystemX86  
C:\Windows\system32

Windows  
C:\Windows


Note: Above given path are generated on Windows 7 32 bit version, using visual studio 2010.
UserName represents the name of user.

For further details check it out here.

Sunday, February 13, 2011

How to pass Command line arguments in C#

 To pass command line argument, create a windows form application and the Main() method should look like:

       [STAThread]
        static void Main()
        {
                string[] args = Environment.GetCommandLineArgs();
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1(args[1], int.Parse(args[2])));
        }
Here,
  Method Environment.GetCommandLineArgs() accepts arguments for this application.
  new Form1(args[1], int.Parse(args[2])) this code segment is passing the  parameters to the constructor, args[1] is for Name, args[2] is for Year of Birth and missing args[0] contains the current executing exe path.

This is the code segment for the constructor accepting parameters:
 public Form1(string Name, int YearOfBirth)
 {             MessageBox.show(string.Format(" NAME :: {0} \n Your year of Birth :: {1}",      
                 Name,YearOfBirth.ToString()));
  }
        
For testing goto command prompt and execute:
 TestArguments.exe "Ahmed" 1980
Or,

 System.Diagnostics.Process.Start(@"TestArguments.exe", "Ahmed " + " 1980" );
    




Saturday, February 12, 2011

How to set Image.Source in WPF?


I found this information to be useful for newbie, as setting up the image source from the code is not straight forward

in WPF Application. Consider following XAML
XAML::
<image Name="imgApplication" HorizontalAlignment="Center" Source="myApp.png" Margin="0,0,17,0" VerticalAlignment="Center" Stretch="None"/>

here if we want to change this "imgApplication" image with some other image from code we can use following code.


Code::
ImageSourceConverter c = new ImageSourceConverter();
imgApplication.Source = (ImageSource)c.ConvertFromString(@"C:\myNewApp.png");

Here,
@"C:\myNewApp.png" specifies the path to the new image.


By Resource Image:


In the same way if you have added the image in Resource then you can use following code:
imgApplication.Source = BitmapSourceFromImage(Properties.Resources.myNewAppPNG);

Here,
"myNewAppPNG" is the name of the image added in resource.
"BitmapSourceFromImage()" is a function to convert the image to type "ImageSource"

public BitmapSource BitmapSourceFromImage(System.Drawing.Image img)
{
      System.IO.MemoryStream memStream = new System.IO.MemoryStream();
      //save the image to memStream as a png
      img.Save(memStream, System.Drawing.Imaging.ImageFormat.Png);

      //gets a decoder from this stream
      System.Windows.Media.Imaging.PngBitmapDecoder decoder = new
              System.Windows.Media.Imaging.PngBitmapDecoder( memStream,
              System.Windows.Media.Imaging.BitmapCreateOptions.PreservePixelFormat,  
              System.Windows.Media.Imaging.BitmapCacheOption.Default);
        return decoder.Frames[0];
}

That's it.