Aug 24 2010

Getting to my Isolated Files (Windows Phone 7)

Category: MobileJoel Ivory Johnson @ 10:58

Download the Code (211 Kilobytes)

A frequent question I see in the MSDN forums is how does one copy their files from Isolated Storage in Windows Phone 7 to their desktop computer. The file system on WP7 isn't exposed so unlike WM6.x you can't just connect it to your desktop and get to any and every file. The general answer given is to create a service that will write files to your desktop's storage and have your WP7 application send the data to that application. There are a lot of ways to do this, so this answer hasn't been enough for some users. I put together this example as an illustration of one of the many ways in which this solution can be built.

In the solution attached to this article you'll find three projects. One project is a Windows Phone 7 application that is made to send data to a service. Another project is the implementation for that service. And the last project is a WPF based GUI that does nothing more than host the service (alternatively the service could have been hosted in IIS or in a service application).

The service has two methods: CopyString(String fileName, string content) for copying string data and CopyBytes(string fileName, byte[] content) for copying binary data

To run the example you would need to start both the WPF application on your desktop (to receive the files) and the application on the phone. On the phone you will enter the name or IP address of your machine (So the phone knows what machine to send the file to) and the port on which the service runs (Default 8001). When you press the upload button on the phone within a few seconds you should see the file's name listed on the PC. The file will be in My Documents\WP7Files\.

If you want to change the port over which the application works you can edit the file that ends with .config in the WPF application.

In this first version of this solution my goal was to only make it work. In a future version I plan to add the ability to transfer large files in chunks, transfer everything in isolated storage, support copying files in both directions, and add security.

This isn't code that you would run in a production environment. Since it is communicating over HTTP instead of HTTPS you don't want to send any sensative data. Currently there's also nothing stopping a rouge client from sending malicious files to the computer (hence the need to add security).

This entry is more geared to some one that has some familiarity with using services in .Net. I'll write step by step instructions for every one else after I further refine this code. If you wanted your own WP7 application to use the service you would need to first run the service locally and then from within Visual Studio right-click on your Windows Phone project, select "Add Service Reference" and type http://localhost:8001/FileCopy/mex/ to add the service reference to your app.

I'll close with the code that was used in the example to read the configuration and then upload a file to the machine identified by the configuration.

public void BeginUpload()
    if (IsUploading)
    string serviceAddress = string.Format("http://{0}:{1}/FileCopy/", this.MachineName, TargetPort);
    ChannelFactory myChannelFactory = new ChannelFactory(new BasicHttpBinding(), new EndpointAddress(serviceAddress));
    //_copyServiceClient = myChannelFactory.CreateChannel(new EndpointAddress(serviceAddress));

    var copyServiceClient = new CopyServiceClient(new BasicHttpBinding(), new EndpointAddress(serviceAddress));
        var encoder = System.Text.UnicodeEncoding.Unicode;
        var byteList = encoder.GetBytes(Content);
        copyServiceClient.CopyBytesCompleted += new EventHandler(copyServiceClient_CopyBytesCompleted);
        copyServiceClient.CopyBytesAsync(FileName, byteList);
        IsUploading = true;
        copyServiceClient.CopyStringCompleted += new EventHandler(copyServiceClient_CopyStringCompleted);

Tags: ,


Ben McCormack Ben McCormack United States says:

I really enjoyed seeing your demo at the Atlanta Windows Phone 7 Firestarted and Garage event.  I can't wait to see the full write-up!

trackback Alberto Silva says:

Windows Phone 7–Experiência para programadores, XXXV

Depois de uns dias fora com a família, o volume de links para artigos relacionados com o desenvolvimento

trackback Oleksandr Krakovetskiy blog says:

Дайджест технических материалов #5 (Windows Phone 7)

Tools, Books, Guides Windows Phone 7 Developer Tools RTM (online installer) , ISO UI Design and Interaction