需求是有两个局域网(各有一台服务器,IP不固定),外网一台服务器(固定IP)
然后,三台服务器,都有一套相同的WEBFORM(WINFORM)程序运行 ,局域网内的机器,用户是有上传附件, 如何借助与外网的主机,保持这三台服务器的附件文件保持一致,局域网有可能出现长时间与外网服务器连不上的情况,两个局域网的两台服务器不能直接相连 (附件是不允许删除和修改,只有添加附件的功能) ;
迅速搭建SVN环境 请参考http://www.cnblogs.com/mouhong-lin/archive/2008/08/01/1258345.html
SVN的客户端"乌龟"支持命令行.于是想到了,使用C#中的文件监控类,监控到站点根目录下面的文件数据,如果有发生变化,那就通知SVN进行添加,然后再间隔固定的时候去提交或者更新.以便上传和下载数据.使用SVN的好处是,该软件支持原子操作,就是要么上传或下载,不用去考虑断点续传相关的程序.所以选择使用SVN而不使用FTP.
窗体控件添加如下图
相关代码如下:
private void button1_Click( object sender, EventArgs e)
{
try
{
if (string.IsNullOrEmpty(label2.Text.Trim())) { MessageBox.Show("请选择监控目录!"); return; }
button1.Enabled = false;
int x = Convert.ToInt32(textBox1.Text.Trim());
if (x < 5) { MessageBox.Show("请输入大于5的正整数!"); button1.Enabled = true ; return; }
timer1.Interval = x *1000* 60;//10000;
timer1.Enabled = true;
button5.Enabled = true;
}
catch
{
// MessageBox.Show("请输入正确的整数!");
button1.Enabled = true;
timer1.Enabled =false ;
}
}
public static string Execute( string dosCommand, int outtime)
{
string output = "";
if (dosCommand != null && dosCommand != "")
{
Process process = new Process();
ProcessStartInfo startinfo = new ProcessStartInfo();
startinfo.FileName = "cmd.exe";
startinfo.Arguments = "/c" + dosCommand;
startinfo.UseShellExecute = false;
startinfo.RedirectStandardInput = false;
startinfo.RedirectStandardOutput = true;
startinfo.CreateNoWindow = true;
process.StartInfo = startinfo;
try
{
if (process.Start())
{
if (outtime == 0)
{ process.WaitForExit(); }
else
{ process.WaitForExit(outtime); }
output = process.StandardOutput.ReadToEnd();
}
}
catch (Exception e)
{
throw e;
}
finally
{
if (process != null)
{ process.Close(); }
}
}
MessageBox.Show(output);
return output;
}
private void button2_Click( object sender, EventArgs e)
{
if (DialogResult.OK == folderBrowserDialog1.ShowDialog())
{
label2.Text = folderBrowserDialog1.SelectedPath;
}
}
private void fileSystemWatcher1_Changed( object sender, System.IO.FileSystemEventArgs e)
{
// MessageBox.Show("改变了" + e.ChangeType.ToString() + e.FullPath);
try
{
ProcessStartInfo psi = new ProcessStartInfo("cmd");
psi.RedirectStandardOutput = true;
psi.RedirectStandardInput = true;
psi.UseShellExecute = false;
Process p = Process.Start(psi);
//改变命令行目录
p.StandardInput.WriteLine(@"chdir /d C:\Program Files\VisualSVN Server\bin");
//有新上传附件,则把新附件置为添加状态
p.StandardInput.WriteLine(@"svn add " + e.FullPath);
p.StandardInput.WriteLine(@"exit");
p.WaitForExit();
p.Close();
}
catch { }
finally
{
}
}
private void button3_Click( object sender, EventArgs e)
{
if (string.IsNullOrEmpty(label2.Text.Trim())) { MessageBox.Show("请选择监控目录!"); return; }
button2.Enabled = false;
button3.Enabled = false;
//要监视的目录
string sFolder = label2.Text;
fileSystemWatcher1.EnableRaisingEvents = true;
fileSystemWatcher1.Path = sFolder;
fileSystemWatcher1.IncludeSubdirectories = true;
fileSystemWatcher1.NotifyFilter = NotifyFilters.Size;
label3.Text = "正在监视中";
button4.Enabled = true;
}
private void button4_Click( object sender, EventArgs e)
{
//timer1.Enabled = false;
fileSystemWatcher1.EnableRaisingEvents = false;
button3.Enabled = true;
button2.Enabled = true;
label3.Text = "未开始监控";
}
private void button5_Click( object sender, EventArgs e)
{
timer1.Enabled = false;
button1.Enabled = true;
}
private void timer1_Tick( object sender, EventArgs e)
{
try
{
ProcessStartInfo psi = new ProcessStartInfo("cmd");
psi.RedirectStandardOutput = true;
psi.RedirectStandardInput = true;
psi.UseShellExecute = false;
Process p = Process.Start(psi);
p.StandardInput.WriteLine(@"chdir /d C:\Program Files\VisualSVN Server\bin");
p.StandardInput.WriteLine("svn commit -m \" "+System.DateTime.Now.ToString()+"\" " + label2.Text.Trim() );//提交(上传)目录下面的改变文件
p.StandardInput.WriteLine(@"exit");
p.WaitForExit();
p.Close();
label4.Text = "上次上传时间:" + System.DateTime.Now.ToString();
}
catch { }
finally
{
}
}
private void button6_Click( object sender, EventArgs e)
{
try
{
if (string.IsNullOrEmpty(label2.Text.Trim())) { MessageBox.Show("请选择监控目录!"); return; }
button6.Enabled = false;
int x = Convert.ToInt32(textBox2.Text.Trim());
if (x < 5) { MessageBox.Show("请输入大于5的正整数!"); button6.Enabled = true ; return; }
timer2.Interval = x * 1000 * 60;//10000;
timer2.Enabled = true;
button7.Enabled = true;
}
catch
{
MessageBox.Show("请输入正确的整数!");
button7.Enabled = true;
timer2.Enabled = false;
}
}
private void button7_Click( object sender, EventArgs e)
{
timer2.Enabled = false;
button6.Enabled = true;
}
private void timer2_Tick( object sender, EventArgs e)
{
try
{
ProcessStartInfo psi = new ProcessStartInfo("cmd");
psi.RedirectStandardOutput = true;
psi.RedirectStandardInput = true;
psi.UseShellExecute = false;
Process p = Process.Start(psi);
p.StandardInput.WriteLine(@"chdir /d C:\Program Files\VisualSVN Server\bin");
//更新(下载)服务器上的最新版本
p.StandardInput.WriteLine("svn update " + label2.Text.Trim());
p.StandardInput.WriteLine(@"exit");
p.WaitForExit();
p.Close();
label7.Text = "上次下载时间:" + System.DateTime.Now.ToString();
}
catch { }
finally
{
}
}
private void button8_Click( object sender, EventArgs e)
{
Application.Exit();
}
SVN命令行支持执行命令时发送用户名和密码.
相关的命令行参数如下 --username USER --password PASS .