There are several files I need to access and update from practically any computer I usemy web browser favorites, contact lists, web-site passwords, and my .NET programming tips. Although I keep these files on a flash drive, it's not always convenient to take the drive everywhere I go. I'd rather store the files on a server and access them over the Internet, as long as it's reliable, secure, and inexpensive. Xdrive.com offers 5 GB of Internet-accessible storage, but the $10.00 monthly fee is more than I pay for hosting my entire web site. Fortunately, Amazon's S3 web service offers secure Internet file access at modest prices. Additionally, .NET 2.0's FtpWebRequest class offers access by programs to FTP sites. In this article, I show how to use S3 and FtpWebRequest to securely synchronize files on multiple machines.
To illustrate, I use Visual Studio 2005 to develop a sample application named "Synchronize Files" (available at http://www.ddj.com/code/). You can build it with Visual Studio 2005 or Visual C# 2005 Express Edition (http://msdn.microsoft.com/vstudio/express/visualcsharp). This program encrypts files for security, and compresses them to conserve bandwidth and storage space. It takes advantage of the Microsoft Enterprise Library for cryptography, logging, and exception management.
Building and Running The Sample App
The sample app uses the Cryptography, Exception Handling, and Logging "Application Blocks" from the January 2006 Microsoft Enterprise Library for .NET Framework 2.0. Before building the app, download the Enterprise Library (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnpag2/html/entlib2.asp) and run the install. After installing the library, specify an encryption key to encrypt/decrypt your files. To specify your key, first ensure the sample app's app.config file is writable. Run the Enterprise Library Configuration program. Select File/Open Application and open the sample app's app.config file. Right-click the Symmetric Provider on the left and select Remove. Then right-click Symmetric Providers and select New/Symmetric Algorithm Provider. Select RijndaelManaged and press OK. Make sure the Create a New Key radio button is selected, then click Next. Click the Generate button, then Next. Specify the key file path and press Next. Specify the data-protection mode and press Finish. On the right, change the Name from "RijndaelManaged" to "Symmetric Provider," which is the name the sample app expects. Finally, select File/Save Application.
Now it's time to build the app. Open SynchronizeFiles.sln in Visual Studio 2005, build the app, and run it. Select Options/Settings and select the S3 or FTP radio button to specify where your files will be stored. If you're using S3, enter the Access Key ID and Secret Access Key you received when signing up. Then press Get Canonical User ID and your Canonical User ID and Display Name is filled in. For an FTP site, first use an FTP program to create a folder on the FTP site for your home directory. Then go back to Synchronize Files and enter the FTP site (ftp://upload.comcast.net, for instance), the home directory you created, and your User ID and Password.
After entering your configuration information, press OK. Click the New button to create a bucket (folder) to hold your files. Then click Upload to upload your files. When files are selected by checking the checkbox in the DataGridView's leftmost column (Figure 1), they can be downloaded or deleted. When you press the Synchronize button, all files with a check in the Synchronize column are uploaded to the server if they've changed on the local machine.