Send mail to the author(s)

    August 12, 2008

    HOWTO: Prevent ResolveAssemblyReferences warnings in Visual Studio 2008

    When you reference an assembly compiled against .NET Compact Framework 2.0 in a project targeting .NET Compact Framework, it's quite likely you will receive a number of ResolveAssemblyReferences warnings, similar to this:

    ResolveAssemblyReferences:
      Consider app.config remapping of assembly "System.Windows.Forms, Culture=neutral, 
      PublicKeyToken=969db8053d3322ac, Retargetable=Yes" from Version "2.0.0.0" [] to 
      Version "3.5.0.0" [C:\Program Files\Microsoft.NET\SDK\CompactFramework\v3.5\
      WindowsCE\System.Windows.Forms.dll] to solve conflict and get rid of warning.

    This warning occurs when the compiler has loaded the .NET Compact Framework 3.5 base class libraries (BCLs) and the referenced assembly, but the metadata contained in the referenced assembly references the .NET Compact Framework 2.0 BCLs.

    To remove the warnings you can add an App.Config to your project and redirect the assembly bindings from the 2.0 versions to the 3.5 equivalents. Below is an example of the XML required in the App.Config to redirect the 2.0 version of System.dll to the 3.5 version.

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <runtime>
        <assemblybinding xmlns="urn:schemas-microsoft-com:asm.v1">
          <dependentassembly>
            <assemblyidentity name="System" culture="neutral" publickeytoken="969db8053d3322ac" />
            <bindingredirect  newVersion="3.5.0.0" oldVersion="2.0.0.0" />
          </dependentassembly>
        </assemblybinding>
      </runtime>
    </configuration>

    You will need to add a dependentassembly element for each of the ResolveAssemblyReferences warnings that you receive.

    Make sure the Build Action property of the App.Config is set to Content and the Copy To Output Directory property is set to Only if newer. Then, rebuild your solution and the warnings should not appear.

    July 23, 2008

    Lambda expressions in .NET Compact Framework 2.0

    There's been a lot of internal discussion today on how to leverage C# 3.0 features without requiring .NET Compact Framework 3.5. Mark has the low down on using extension methods with a little compiler misdirection hack.

    The discussions kicked off because I discovered that you can use lambda expressions with .NET Compact Framework 2.0 projects in Visual Studio 2008. I even twittered about it yesterday. (Note: it didn't take me too long to realize it's not a bug, but due to Visual Studio 2008 using the C# 3.0 compiler.) Code like the following just works -- no hacks required:

    delegate double PowerOf(double x, double y);
    
    static void Main() 
    {
        PowerOf pwr = (x, y) => Math.Pow(x, y);
        double result = pwr(2, 3);
    }

    If you are using Visual Studio 2008 and still need to target .NET Compact Framework 2.0, the little things like this go a long way to creating and maintaining reusable code.

    July 16, 2008

    OpenNETCF is now a Microsoft Gold Partner

    Over the last few months we've been working hard to attain the Gold Level partner status in the Microsoft Partner Program and I'm extremely pleased to say that we've done it! For our customers, this is an assurance that by choosing to work with OpenNETCF products and our consultants, you really are choosing a high calibre company.

    Massive amounts of credit for Mark for driving this internally. Good job!

    July 11, 2008

    Mark your calendars - Smart Device Development Chat, July 16.

    The next Smart Device Development Chat will take place on MSDN at 10am PST on Wednesday, July 16.

    Please join experts from the Windows Mobile, Windows CE, SQL Server CE and .NET Compact Framework communities in a chat around application development for smart devices. These chats are a great opportunity to have your questions answered by experts from around the world.

    Add to Calendar

    If you cannot attend the MSDN Chat, there is a permanent chat room available on the web and via IRC (irc://irc.freenode.net/mobiledev). Engage with your peers online today!

    June 24, 2008

    HOW-TO: Disable/Enable Network Connections Programmatically under Vista

    I got an email last week asking about how to disable a particular network connection under Vista. The specific scenario, how to disable an active 3G connection, is not something I'm going to cover, but what I present below could be used as basis for that scenario.

    With Vista, Microsoft introduced two new methods to the Win32_NetworkAdapter class under WMI: Enable and Disable. Before can call either of those methods, we need to know how to enumerate the network connections.

    The .NET Framework SDK provides a helpful utility called mgmtclassgen.exe, which can be used to create .NET-friendly wrappers of the WMI classes. Open up a Visual Studio command prompt and enter the following:

    mgmtclassgen Win32_NetworkAdapter -p NetworkAdapter.cs

    This will generate a file called NetworkAdapter.cs which will contain a C# representation of the WMI Win32_NetworkAdapter class. You can add this source code file to your C# project and then access all the properties without too much extra effort.

    To filter and disable the specific adapters, you do something like this:

    SelectQuery query = new SelectQuery("Win32_NetworkAdapter", "NetConnectionStatus=2");
    ManagementObjectSearcher search = new ManagementObjectSearcher(query);
    foreach(ManagementObject result in search.Get())
    {
        NetworkAdapter adapter = new NetworkAdapter(result);
    
        // Identify the adapter you wish to disable here. 
        // In particular, check the AdapterType and 
        // Description properties.
    
        // Here, we're selecting the LAN adapters.
        if (adapter.AdapterType.Equals("Ethernet 802.3")) 
        {
            adapter.Disable();
        }
    }

    Don't forget to add a reference to System.Management.dll!