Thursday, September 4, 2008

Window CE, how to get File size, version information in C#?

For file size, length, last write etc, you can use FileInfo as following:

FileInfo aa = new FileInfo("\\xxx");
aa.LastWriteTime;
aa.Length;
aa.CreationTime;

for file version, you have to use some C++ help here:

public class FileVersionInfo
{
#region Variables

private string m_sFileName;
private byte[] m_bytVersionInfo;

#endregion

#region Constants

private const int GMEM_FIXED = 0x0000;
private const int LMEM_ZEROINIT = 0x0040;
private const int LPTR = (GMEM_FIXED | LMEM_ZEROINIT);

#endregion

#region Constructors

///
/// Constructor.
///

/// File name and path.
private FileVersionInfo(string sFileName)
{
if (File.Exists(sFileName))
{
int iHandle = 0;
int iLength = 0;
int iFixedLength= 0;
IntPtr ipFixedBuffer = IntPtr.Zero;

// Get the file information.
m_sFileName = Path.GetFileName(sFileName);
iLength = GetFileVersionInfoSize(sFileName, ref iHandle);

if(iLength > 0)
{
// Allocate memory.
IntPtr ipBuffer = AllocHGlobal(iLength);

// Get the version information.
if(GetFileVersionInfo(sFileName, iHandle, iLength, ipBuffer))
{
// Get language independant version info.
if(VerQueryValue(ipBuffer, "\\", ref ipFixedBuffer, ref iFixedLength))
{
// Copy information to array.
m_bytVersionInfo = new byte[iFixedLength];
Marshal.Copy(ipFixedBuffer, m_bytVersionInfo, 0, iFixedLength);
}
}

// Free memory.
FreeHGlobal(ipBuffer);
}
}
else
{
m_bytVersionInfo = new byte[200];
}
}

#endregion

#region Properties

///
/// Get the file build part.
///

public int FileBuildPart
{
get
{
return Convert.ToInt32(BitConverter.ToInt16(m_bytVersionInfo, 14));
}
}

///
/// Get the file major part.
///

public int FileMajorPart
{
get
{
return Convert.ToInt32(BitConverter.ToInt16(m_bytVersionInfo, 10));
}
}

///
/// Get the file minor part.
///

public int FileMinorPart
{
get
{
return Convert.ToInt32(BitConverter.ToInt16(m_bytVersionInfo, 8));
}
}

///
/// Get the name of the file.
///

public string FileName
{
get
{
return m_sFileName;
}
}

///
/// Get the file private part.
///

public int FilePrivatePart
{
get
{
return Convert.ToInt32(BitConverter.ToInt16(m_bytVersionInfo, 12));
}
}

///
/// Get the product build part.
///

public int ProductBuildPart
{
get
{
return Convert.ToInt32(BitConverter.ToInt16(m_bytVersionInfo, 22));
}
}

///
/// Get the product major part.
///

public int ProductMajorPart
{
get
{
return Convert.ToInt32(BitConverter.ToInt16(m_bytVersionInfo, 18));
}
}

///
/// Get the product minor part.
///

public int ProductMinorPart
{
get
{
return Convert.ToInt32(BitConverter.ToInt16(m_bytVersionInfo, 16));
}
}

///
/// Get the product private part.
///

public int ProductPrivatePart
{
get
{
return Convert.ToInt32(BitConverter.ToInt16(m_bytVersionInfo, 20));
}
}

#endregion

#region Functions

///
/// Allocate unmanged memory.
///

/// Length to allocate.
/// IntPtr object.
private static IntPtr AllocHGlobal(int iLength)
{
return LocalAlloc(LPTR, (uint)iLength);
}

///
/// Free allocated memory.
///

/// IntPtr object to free.
private static void FreeHGlobal(IntPtr hGlobal)
{
LocalFree(hGlobal);
}

///
/// Get the file version information.
///

/// File name and path.
/// FileVersionInfo object.
public static FileVersionInfo GetVersionInfo(string sFileName)
{
return new FileVersionInfo(sFileName);
}

#endregion

#region Win32API

[DllImport("coredll", EntryPoint="GetFileVersionInfo", SetLastError=true)]
private static extern bool GetFileVersionInfo(
string filename,
int handle,
int len,
IntPtr buffer);

[DllImport("coredll", EntryPoint="GetFileVersionInfoSize", SetLastError=true)]
private static extern int GetFileVersionInfoSize(
string filename,
ref int handle);

[DllImport("coredll.dll", EntryPoint="LocalAlloc", SetLastError=true)]
private static extern IntPtr LocalAlloc(
uint uFlags,
uint Bytes);

[DllImport("coredll.dll", EntryPoint="LocalFree", SetLastError=true)]
private static extern IntPtr LocalFree(
IntPtr hMem);

[DllImport("coredll", EntryPoint="VerQueryValue", SetLastError=true)]
private static extern bool VerQueryValue(
IntPtr buffer,
string subblock,
ref IntPtr blockbuffer,
ref int len);

#endregion
}
}

Then:

FileVersionInfo fiWindows = FileVersionInfo.GetVersionInfo(@"\Windows\sdcgina.exe");
//MessageBox.Show("1 Window:" + fiWindows.FileMajorPart + "_" + fiWindows.FileMinorPart + "_" + fiWindows.FileBuildPart + "_"
// + fiWindows.ProductMajorPart + "_" + fiWindows.ProductMinorPart + "_" + fiWindows.ProductPrivatePart + "_" + fiWindows.ProductBuildPart
// + "SystemCF:" + fiSystemCF.FileMajorPart + "_" + fiSystemCF.FileMinorPart + "_" + fiSystemCF.FileBuildPart + "_"
// + fiSystemCF.ProductMajorPart + "_" + fiSystemCF.ProductMinorPart + "_" + fiSystemCF.ProductPrivatePart + "_" + fiSystemCF.ProductBuildPart);

No comments: