5,663,486 members and growing! (17,480 online)
Email Password   helpLost your password?
Languages » C# » Utilities     Intermediate License: The Code Project Open License (CPOL)

FtpPublisher, An FTP File Synchronization Tool

By Thomas Guilbault

A small utility that will upload changed files to an FTP site
C# 2.0, C#, Windows, .NET, .NET 2.0VS2005, Visual Studio, Dev

Posted: 12 Oct 2006
Updated: 12 Oct 2006
Views: 23,473
Bookmarked: 37 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
5 votes for this Article.
Popularity: 2.96 Rating: 4.24 out of 5
0 votes, 0.0%
1
0 votes, 0.0%
2
0 votes, 0.0%
3
4 votes, 80.0%
4
1 vote, 20.0%
5
Sample Image - FtpPublisher.jpg

Introduction

I wrote this utility when I found myself having to repeatedly edit files for a Web site and then use an FTP client to upload the changes. Since I was spending my time editing images, I had to switch between multiple programs and then perform multiple steps to get the changed files uploaded. This made me want a simpler way.

FtpPublisher removes the need to manually use an FTP client or have another program that you use to publish with FTP. It will support any number of files, from various local locations to be synchronized to the remote site.

Using FtpPublisher

FtpPublisher has two modes, a design and an execution mode. As it sounds like, design mode lets you set your configuration while execution mode will take the configuration and perform the date testing and uploading.

Once your FTP connection information has been entered, FtpPublisher will retrieve the site's folder hierarchy. Each remote folder can contain a list of files that will be synchronized. This configuration can be saved into an XML file, which FtpPublisher takes as a command line argument to run in its execution mode.

To further increase the efficiency of the application, it can create a file association with its own file type. This is done by running the InstallUtil on the executable. If this is done, uploading your changes to an FTP site is as simple as double clicking the configuration file and watching it work.

The Code

FTP

All FTP functions come from edtFTPnet. I didn't use .NET 2.0's FtpWebRequest because on my initial examination, I was blind and didn't see a way for it to run in passive mode. I later looked again and realized I just hadn't scrolled down far enough, but the application was finished by then.

File Synchronization

Every file entry in the configuration contains a path and a DateTime stamp for when that file was uploaded last. It decides to upload if the file has been modified since it last wrote the stamp. I chose this method instead of getting the DateTime from the FTP because I wanted these local files to be the master copies.

if (File.GetLastWriteTime(item.Path) > item.LastUpload)

Thread Synchronization with Anonymous Methods

The execution mode has its own UI. To keep that UI responsive, I have a BackgroundWorker do all the main work on a separate thread. I couldn't use its built in progress report functionality because it performs multiple types of progress updating. To simplify my life, I originally used anonymous methods inside a Control Invoke call.

progressBarMain.Invoke((MethodInvoker)delegate() { });

As I was coding, I wondered if there was another way to ensure the updates were run on the correct thread. This is where SynchronizationContext comes in. It is a class designed to handle the details of which thread to perform on.

It works by getting a reference to the current thread when the Current property is accessed.

SynchronizationContext context = SynchronizationContext.Current;

Then, inside the background thread, a call to its Send method will perform the work.

context.Send(delegate(object state) { }, null);

Installer

Setting up file associations is an easy thing to do, but some people might not know the details of how it's done. So here is an example:

public static void PerformInstall(string path)
{
    if (File.Exists(path)) 
    {
        RegistryKey key = Registry.ClassesRoot.CreateSubKey(".cFtp");
        key.SetValue("", "cFtpFileType", RegistryValueKind.String);
        key.SetValue("Content Type", "text/xml", RegistryValueKind.String);

        key = Registry.ClassesRoot.CreateSubKey("cFtpFileType");
        key.SetValue("", "Ftp Publisher Configuration", 
                     RegistryValueKind.String);
        
        RegistryKey icon = key.CreateSubKey("DefaultIcon");
        icon.SetValue("", string.Format("{0}", 
                      Path.Combine(Path.GetDirectoryName(path), "Icon.ico")), 
                      RegistryValueKind.String);

        RegistryKey cmd = key.CreateSubKey("shell\\open\\command");
        cmd.SetValue("", string.Format("\"{0}\" \"%1\"", path), 
                     RegistryValueKind.String);
    }
}

Notes

  • The configuration files store user names and passwords unencrypted.

History

  • 10-12-2006: Original article

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Thomas Guilbault


Thomas is developer for a small service company based in the greater Vancouver area of canada. He has spent most of his time building custom c# business applications for clients.
Occupation: Software Developer
Location: Canada Canada

Other popular C# articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 12 of 12 (Total in Forum: 12) (Refresh)FirstPrevNext
GeneralFile Flushing in C#( dot net)memberShantonu2:03 20 Oct '08  
QuestionRemote Folder List using FtpWebRequest?memberalias4718:41 14 May '07  
AnswerRe: Remote Folder List using FtpWebRequest?memberThomas Guilbault19:24 14 May '07  
GeneralUse of the ftpclient in a commercial product...memberskdutta15:47 24 Apr '07  
GeneralRe: Use of the ftpclient in a commercial product...memberThomas Guilbault16:51 24 Apr '07  
QuestionPossibility to use QUOTE RCMD SBMJOB CMD()?memberpaoloden1:58 6 Jan '07  
AnswerRe: Possibility to use QUOTE RCMD SBMJOB CMD()?memberThomas Guilbault13:55 6 Jan '07  
GeneralFTP Based Source ControlmemberHoyaSaxa9312:37 13 Oct '06  
GeneralRe: FTP Based Source ControlmemberThomas Guilbault15:55 14 Oct '06  
Generalhow the hell do i add a folder !?memberhacx2k623:07 12 Oct '06  
GeneralRe: how the hell do i add a folder !?memberThomas Guilbault12:11 13 Oct '06  
GeneralRe: how the hell do i add a folder !?memberhacx2k623:59 13 Oct '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 12 Oct 2006
Editor: Deeksha Shenoy
Copyright 2006 by Thomas Guilbault
Everything else Copyright © CodeProject, 1999-2008
Web13 | Advertise on the Code Project