How To: Load Image from Embedded Resource

Here is a code for loading embedded image named MyImage.png stored directly in project directory:

Assembly assembly = Assembly.GetExecutingAssembly();
string iconFileName = string.Format("{0}.MyImage.png", assembly.GetName().Name);
using (Stream io = assembly.GetManifestResourceStream(iconFileName)) {
	return new Bitmap(io);
}

Name of resource to load contains three elements divided by dots:

  • Assembly name;
  • Path where resource is stored inside of a project (must be omitted if resource is in project directory);
  • File name of resource.

Path to resource should be created using names of folders. For example, if resource is in “Resources/Images” folder than path will be “Resources.Images”.

How To: Save XmlDocument without Declaration

Normally saving XmlDocument leads to xml declaration at the beginning of file. In case it is desirable to omit it, XmlWriterSettings comes to help:

XmlDocument doc = new XmlDocument();
// Do with document whatever is needed...

// Save xml document without xml declaration.
XmlWriterSettings writerSettings = new XmlWriterSettings();
writerSettings.OmitXmlDeclaration = true;
using (XmlWriter writer = XmlWriter.Create(@"\omit.xml", writerSettings)) {
	doc.Save(writer);
}

How To: Make File Writable on Windows Mobile

Making read-only file writable with full .Net Framework is a piece of cake: there is FileInfo.IsReadOnly. Just set it to false and it’s done.

With Compact Framework we need to use FileInfo.Attributes and a solid knowledge of bitwise operations:

FileInfo info = new FileInfo(pathToReadonlyFile);
info.Attributes &= ~FileAttributes.ReadOnly;

How To: Check That Application is Running Under Windows Mobile 6.5

As I wrote in Linkpool: Windows Mobile 6.1 and 6.2 Build Numbers they have the same version (which by the way is 5.2). So checking if application is running under WM 6.5 must be based on build number. Like this:

Version osVersion = Environment.OSVersion.Version;
if (osVersion.Major == 5 && osVersion.Minor == 2 && osVersion.Build >= 21139) {
    // It's Windows Mobile 6.5
}

How To: Connect Windows CE 5.0 Emulator to Visual Studio 2005

Emulator from Windows CE 5.0 SDK can be easily used as a standalone device emulator, but if you want to use it for development it has to be connected to Visual Studio:

1. First of all you need to add “/Ethernet virtualswitch” parameter to target of emulator shortcut:

Adding "/Ethernet virtualswitch" parameter to emulator shortcut
2. Now start emulator and add shared folder. To add shared folder use Emulator > Folder Sharing menu.

3. Use shared folder that will appear as Storage Card on emulator to copy all files from “C:\Program Files\Common Files\Microsoft Shared\CoreCon\1.0\Target\wce400\x86″ to Windows folder on emulator. These files should include:

ClientShutdown.exe
ConmanClient2.exe
CMaccept.exe
eDbgTL.dll
TcpConnectionA.dll

4. Run ConmanClient2.exe on emulator by using Start > Run > \Windows\ConmanClient2.exe:

Starting conmanclient2.exe on emulator
5. Get IP address of emulator by double clicking on network icon:

Retrieving emulator IP address
6. Now you can verify emulator’s accessibility by pinging it from command prompt:

Verifying emulator IP address by pinging it
7. It’s time to change Visual Studio setup:

  • Open Visual Studio options and navigate to Device Tools > Devices;
  • Select “Windows CE 5.0 Device” from list of devices for Windows CE 5.0 platform and open it’s properties;
  • Open configuration dialog by clicking on Configure button next to Transport combo box;
  • And finally change IP address and confirm changes.

Select properties of Windows CE 5.0
Opening transport configuration
Change IP address of device
8. Now you need to run CMaccept.exe from Start > Run > \Windows\CMaccept.exe in order to change security on emulator.

9. And here comes most exciting moment. Now you can run your .Net CF project from Visual Studio. First run will take some time because Visual Studio will deploy and install .Net Compact Framework before running your application. Does it work? It should be, but you’ll need to pass one more mile to make environment more robust.

10. To avoid running CMaccept.exe every time you’ll start emulator you need to add special value to device’s register:

  • Start Remote Registry Editor from Visual Studio Remote Tools;
  • Select “Windows CE 5.0 Device” in device selection window and click OK. In case you have problems with connecting to emulator run CMaccept.exe right before hitting OK button;
  • Navigate to HKLM > System key in device registry;
  • Add new DWORD value in this key named “CoreConOverrideSecurity” with value 1.

11. The last thing is to save state of the emulator and make sure it will be restored on next start:

  • Close emulator using “Save state”;
  • Navigate to “My Documents\My Virtual Machines” on your computer;
  • Find folder named like {641834F7-7BD7-4A7B-B2CE-D11A2C48E93E}. If you have several folders here, find the one updated the last (by the way besides .vmc file it will contain .vsv with saved state);
  • Add state restoring parameter to target of emulator shortcut that looks like this “/vmid {641834F7-7BD7-4A7B-B2CE-D11A2C48E93E}”, but using your your folder name.

Congratulations! Enjoy development for Windows CE 5.0!

How To: Check Whether String is a Valid Path String

Sometimes it’s needed to verify if given string with path is valid in terms of syntax to make sure that we can work with it (meaning it doesn’t contain symbols not allowed in path). This can be done pretty simply:

bool isValid = (path2Check.IndexOfAny(Path.GetInvalidPathChars()) == -1);

How To: Determine What Version of .Net CF is Installed on Device

List of .Net Compact Framework versions installed on device are available in registry under:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETCompactFramework

This key contains a set of DWORD values with names in format X.Y.BBBB.mm that correspond to build numbers of .Net CF (build numbers can be found in Wikipedia). Each of these values can have data of 0 (installed in RAM) or 1 (installed in ROM). So to verify if specific version of .Net CF is installed we need to check if value with corresponding name exists.

Here is a method that checks for .Net CF 2  (I wrote it today to verify available .Net CF versions in setup.dll of cab installer):

BOOL IsNetCF2Installed() {
	BOOL bNetCF2Found = FALSE;

	HKEY hKey = NULL;
	if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("Software\\Microsoft\\.NETCompactFramework"), 0, KEY_READ | KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS) {
		DWORD dwIndex = 0;
		TCHAR szName[255];
		DWORD cName = 255;
		while ((!bNetCF2Found) && (::RegEnumValue(hKey, dwIndex, szName, &cName, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)) {
			if (szName[0] == '2') {
				bNetCF2Found = TRUE;
			}
			dwIndex++;
		}

		RegCloseKey(hKey);
	}

	return bNetCF2Found;
}

How To: Hard Reset HTC S730

Yesterday HTC S730 that I use for development hang and after soft reset I couldn’t start it again.

So I had to perform hard reset using special key combination: holding both soft buttons press power button. At first it didn’t work. After spending couple of minutes on Internet I found that there is a trick: power button must be pressed only for a second and then released.
After very first try I successfully reset the phone :)

How To: Disable MSN in Outlook Express

When Outlook Express starts it automatically starts MSN. But there is a way to stop this behavior:

  1. Start Registry Edit (Click Start, then Run, type in ‘regedit’ and click OK).
  2. Go to HKEY_LOCAL_MACHINE > Software > Microsoft > Outlook Express .
  3. Create new DWORD value with name “Hide Messenger” and set value to 2.

Now you just need to stop MSN, and restart Outlook Express. You will see that MSN Messenger no longer appear.

How To: Install and Uninstalling Applications on Android Emulator

Story 1. Installing application.

To install application to emulator adb utility will be handy:

  • Start emulator.
  • Open command prompt (cmd.exe).
  • Navigate to “tools” directory in Android SDK.
  • Run installation using command “adb install application.apk”. For example, “adb install “C:/com.andreware.sample.apk”" when com.andreware.sample.apk is located at root directory on disk C.
  • Wait couple of seconds while application is being installing.

Story 2. Uninstalling application.

There two ways to uninstall application from emulator: uninstall like on real device or use adb utility. First way allows to uninstall any application while adb utility will allow to uninstall only application installed using it (including the ones deployed from IDE).

Let’s look at first one. It is pretty simple and straitforward:

  • Start emulator.
  • On the home screen press the “Menu” button.
  • Choose”Settings” item from menu.
  • Choose “Applications” item in the settings list.
  • Choose “Manage applications”.
  • Choose application that you want to uninstall.
  • In the “Application info” window that will appear click “Uninstall” button.
  • Confirm uninstalling.
  • Wait a second while application is being uninstalling and click “OK” on next window that says “Uninstall finished”.

Second way requires usage of command line and adb utility:

  • Start enulator.
  • Open command window (cmd.exe).
  • Navigate to “tools” directory in Android SDK.
  • Run remote shell: “adb shell”.
  • Goto app directory: “cd /data/app”.
  • Now you can list installed applications: “ls”.
  • To uninstall application use “rm” command and appropriate apk file. For example, “rm com.andreware.sample.apk”.
  • Type “exit” to leave remote shell.

I’m Tweeting

Follow

Get every new post delivered to your Inbox.