Commentor Blog

When Quality Matters

Commentor A/S

When Quality Matters

Contact usSend mail

Recent comments

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2012

Copying files from the Desktop to the Device using .NET

I've been frequently asked on how to copy files from the desktop to a Windows Mobile based device. The answer is the Remote API. here's how to do it:

 

First, we'll need our P/Invokes to rapi.dll 

 

[DllImport("rapi.dll")]
static extern int CeRapiInit();
 
[DllImport("rapi.dll")]
static extern int CeRapiUninit();
 
[DllImport("rapi.dll", CharSet = CharSet.Unicode)]
static extern IntPtr CeCreateFile(
  string lpFileName,
  uint dwDesiredAccess,
  int dwShareMode,
  int lpSecurityAttributes,
  int dwCreationDisposition,
  int dwFlagsAndAttributes,
  int hTemplateFile);
 
[DllImport("rapi.dll", CharSet = CharSet.Unicode)]
internal static extern int CeWriteFile(
    IntPtr hFile,
    byte[] lpBuffer,
    int nNumberOfbytesToWrite,
    ref int lpNumberOfbytesWritten,
    int lpOverlapped);
 
[DllImport("rapi.dll", CharSet = CharSet.Unicode)]
internal static extern int CeSetFileTime(
    IntPtr hFile,
    ref long lpCreationTime,
    ref long lpLastAccessTime,
    ref long lpLastWriteTime);
 
const int BUFFER_SIZE = 1024 * 5; // 5k transfer buffer
const int CREATE_ALWAYS = 2;
const int ERROR_SUCCESS = 0;
const int FILE_ATTRIBUTE_NORMAL = 0x80;
const uint GENERIC_WRITE = 0x40000000;
const int INVALID_HANDLE_VALUE = -1;

 

Now let's wrap all those in a method called CopyToDevice(string localFile, string remoteFile). The localFile is the file located on the desktop and the remoteFile is the destination filename on the device.

 

void CopyToDevice(string localFile, string remoteFile)
{
    var rapi = CeRapiInit() == ERROR_SUCCESS;
    if (!rapi)
        return;
 
    try
    {
        var filePtr = CeCreateFile(remoteFile, GENERIC_WRITE, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
        if (filePtr == new IntPtr(INVALID_HANDLE_VALUE))
            return;
 
        using (var localFileStream = new FileStream(localFile, FileMode.Open, FileAccess.Read))
        {
            var byteswritten = 0;
            var position = 0;
            var buffer = new byte[BUFFER_SIZE];
 
            var bytesread = localFileStream.Read(buffer, position, BUFFER_SIZE);
            while (bytesread > 0)
            {
                position += bytesread;
                if (CeWriteFile(filePtr, buffer, bytesread, ref byteswritten, 0) == ERROR_SUCCESS)
                    return;
 
                try
                {
                    bytesread = localFileStream.Read(buffer, 0, BUFFER_SIZE);
                }
                catch
                {
                    bytesread = 0;
                }
            }
        }
    }
    finally
    {
        CeRapiUninit();
    }
}

 

To use the code above you will have to know the full path of the file on the desktop. I hope you found this useful.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: Windows Mobile | RAPI
Posted by christian.resma.helle on Monday, August 23, 2010 9:12 AM
Permalink | Comments (0) | Post RSSRSS comment feed

Retrieving the build history from Team Foundation Server using the Visual Studio 2010 SDK

In this short article I would like to demonstrate how to use the Visual Studio 2010 SDK to retrieve the Build History for a specific Team Project and Build Definition and retrieve the Change Sets and Work Items associated to the build. Before getting started we need to install the Visual Studio 2010 SDK.

The namespaces used in this sample are included in the following assemblies: 

  • Microsoft.TeamFoundationServer.Client
  • Microsoft.TeamFoundationServer.Build.Client -
  • Microsoft.TeamFoundationServer.VersionControl.Client
  • Microsoft.TeamFoundationServer.WorkItemTracking.Client 

 
using System;
using System.Diagnostics;
using Microsoft.TeamFoundation.Build.Client;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
 
namespace BuildHistory
{
    class Program
    {
        static void Main(string[] args)
        {
            Trace.Listeners.Add(new ConsoleTraceListener());
 
            var tfs = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri("[TFS SERVER URL"), new UICredentialsProvider());
            tfs.EnsureAuthenticated();
 
            var buildServer = tfs.GetService<IBuildServer>();
            var versionControl = tfs.GetService<VersionControlServer>();
 
            var buildDetails = buildServer.QueryBuilds("[TEAM PROJECT NAME]", "[BUILD DEFINITION NAME]");
            foreach (var build in buildDetails)
            {
                Trace.WriteLine("Build: " + build.BuildNumber);
 
                var changesets = InformationNodeConverters.GetAssociatedChangesets(build);
                if (changesets == null)
                    continue;
 
                foreach (var summary in changesets)
                {
                    Trace.WriteLine("Changeset Summary: " + summary.ChangesetId + " " + summary.Comment);
 
                    var changesetItem = versionControl.GetChangeset(summary.ChangesetId);
                    foreach (var file in changesetItem.Changes)
                        Trace.WriteLine("Affected file: " + file.Item.ServerItem);
 
                    foreach (var workItem in changesetItem.WorkItems)
                        Trace.WriteLine("Work Item: " + workItem.Id + " " + workItem.Title);
                }
 
                Trace.WriteLine(string.Empty);
            }
 
            Console.ReadLine();
        }
    }
}
 

I hope you found this useful 

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by christian.resma.helle on Wednesday, August 04, 2010 3:34 AM
Permalink | Comments (0) | Post RSSRSS comment feed