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
| You must Sign In to use this message board. |
|
|
 |
|
 |
There is a very easy to use FTP synchronizer, called BestSync. You can download it from http://www.risefly.com . It supports FTP proxy, and can synchronize with FTP server in different time zone, resume the file trasfer from the last break point, file compress and encryption etc.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Hi...to all I am in problem in case of file flushing in run time mode. I can't find the necessary codes & Library in dot net 2.0. My primary target is to flush file(& make the RAM free) during my software running according to my input time. Thanks to help me.
I am very interested with OOP & its modeling.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
I am interested in your treeview Folder List. I want to be able to do something like this in a .NET 2.0 FtpWebRequest implementation. You mentioned that you had had a look at this. Can you see a way of getting a list of remote folders using FtpWebRequest?
Regards Andrew
|
| Sign In·View Thread·PermaLink | 1.00/5 |
|
|
|
 |
|
 |
Each command to the ftp server uses a singe FtpWebRequest. So getting directory contents would be this. There is no built in parser for the response stream, so u get to parse it yrself. And a known thing is that one some commands ftp://contoso.com/path/ will get diffrent info than ftp://contoso.com/path
FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create("ftp://contoso.com/path/"); request.Credentials = new NetworkCredential(User, Password); request.Method = WebRequestMethods.Ftp.ListDirectory; WebResponse response = request.GetResponse(); Stream dataStream = response.GetResponseStream();
|
| Sign In·View Thread·PermaLink | 2.00/5 |
|
|
|
 |
|
 |
Hi!
I like this library and would like to use it in a commercial product for my customer. I looked at codeflex and the license condition says that I need to keep the license information intact. However, in the source code I do not see the license file.
Also, I want to just distribute the dll only. Do I need to make a copy of the license html from codeplex and distribute it too? If so does it need to be compiled etc?
I am just making sure so that I do not put my customer into some future problems.

Thanks SK
sdk123us at yahoo.com
|
| Sign In·View Thread·PermaLink | 5.00/5 |
|
|
|
 |
|
 |
Hi,
A copy of the license was supposed to be included with both the source and demo zip file but I missed the licience when I origionaly made my zip.
The library is under the lgpl so anywhere it goes a copy of the lience is supposed to go too.
comercial products also have to say in them that parts are under that licience and have a quick access to the licience file.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Hello, I'm searching an FTP library, in .NET, which allows sending such a command as of Subject. Yours, can?
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
 |
Just a suggestion for an enhancement... if a team of developers have the need to FTP upload to the same location, this application could be converted into an FTP Source Control application.
- Get Latest version Retrieve a server "manifest" that listed the files the had been altered since the users last refresh
- Check Out Put a lock on a file to that others could not alter and upload the exclusively locked file(s); Check In would remmove the lock after the file had been uploaded
Good app... Thanks for posting the article.
|
| Sign In·View Thread·PermaLink | 1.00/5 |
|
|
|
 |
|
 |
Good suggestions, I think I will add the get latest version funtionality to. That will make it more usefull.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
 |
The program was designed to speed up the upload process when of the same items over and over again, so currently the closest to slecting a folder is to just select all the files inside a folder.
What else did you mean by it doesnt flow?
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
ok, never mind. i think i'll skip this app, i dont understand at all how it works and its too buggy. took me 15 minutes to figure out you have to click "OK" to get the directory list, change it to something more accurate like "CONNECT". why cant i attach master local-directory to a master remote-directory on the server and sync instead of selecting 400 files manually.
Sorry, doesnt "Flow" (its like a puzzle).
|
| Sign In·View Thread·PermaLink | 3.67/5 |
|
|
|
 |
|
|