使用SVN+C#.NET保持多台机器的文件同步

 

   需求是有两个局域网(各有一台服务器,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)
ExpandedBlockStart.gifContractedBlock.gif        
{
           

                        
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
if (string.IsNullOrEmpty(label2.Text.Trim())) { MessageBox.Show("请选择监控目录!"); return; }

                button1.Enabled 
= false;
                
int x = Convert.ToInt32(textBox1.Text.Trim());
ExpandedSubBlockStart.gifContractedSubBlock.gif                
if (x < 5{ MessageBox.Show("请输入大于5的正整数!"); button1.Enabled = true ; return; }

                timer1.Interval 
=   x *1000* 60;//10000;

                timer1.Enabled 
= true;
                button5.Enabled 
= true;

            }

            
catch 
            
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
               
// MessageBox.Show("请输入正确的整数!");

                button1.Enabled 
= true;
                timer1.Enabled 
=false ;
            
            
            }





        }



        
public   static   string  Execute( string  dosCommand,  int  outtime)
ExpandedBlockStart.gifContractedBlock.gif        
{
            
string output = "";
            
if (dosCommand != null && dosCommand != "")
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                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
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    
if (process.Start())
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        
if (outtime == 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
{ process.WaitForExit(); }
                        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
{ process.WaitForExit(outtime); }
                        output 
= process.StandardOutput.ReadToEnd();
                    }

                }

                
catch (Exception e)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    
throw e;
                }

               
                
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    
if (process != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{ process.Close(); }
                }

            }


            MessageBox.Show(output);
            
return output;
        }


        
private   void  button2_Click( object  sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
{
            
if (DialogResult.OK == folderBrowserDialog1.ShowDialog())
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{

                label2.Text 
= folderBrowserDialog1.SelectedPath;
            
            }


        }


        
private   void  fileSystemWatcher1_Changed( object  sender, System.IO.FileSystemEventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
{
            
// MessageBox.Show("改变了" + e.ChangeType.ToString() + e.FullPath);
            try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{

                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();

            }

ExpandedSubBlockStart.gifContractedSubBlock.gif            
catch { }
            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{


            }

          
 

        }


        
private   void  button3_Click( object  sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
{

ExpandedSubBlockStart.gifContractedSubBlock.gif            
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)
ExpandedBlockStart.gifContractedBlock.gif        
{
            
//timer1.Enabled = false;
            fileSystemWatcher1.EnableRaisingEvents = false;
            button3.Enabled 
= true;
            button2.Enabled 
= true;
            label3.Text 
= "未开始监控";

        }


        
private   void  button5_Click( object  sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
{
            timer1.Enabled 
= false;
            button1.Enabled 
= true;
        }


        
private   void  timer1_Tick( object  sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
{
            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{

                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();

            }

ExpandedSubBlockStart.gifContractedSubBlock.gif            
catch { }
            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{


            }

        }


        
private   void  button6_Click( object  sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
{
            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
if (string.IsNullOrEmpty(label2.Text.Trim())) { MessageBox.Show("请选择监控目录!"); return; }

                button6.Enabled 
= false;
                
int x = Convert.ToInt32(textBox2.Text.Trim());
ExpandedSubBlockStart.gifContractedSubBlock.gif                
if (x < 5{ MessageBox.Show("请输入大于5的正整数!"); button6.Enabled = true ; return; }

                timer2.Interval 
= x * 1000 * 60;//10000; 

                timer2.Enabled 
= true;
                button7.Enabled 
= true;

            }

            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                MessageBox.Show(
"请输入正确的整数!");

                button7.Enabled 
= true;
                timer2.Enabled 
= false;


            }

        }


        
private   void  button7_Click( object  sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
{
            timer2.Enabled 
= false;
            button6.Enabled 
= true;
        }


        
private   void  timer2_Tick( object  sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
{
            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{

                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();

            }

ExpandedSubBlockStart.gifContractedSubBlock.gif            
catch { }
            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{


            }

        }


        
private   void  button8_Click( object  sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
{
            Application.Exit();
        }

     

 

   

    SVN命令行支持执行命令时发送用户名和密码.

    相关的命令行参数如下 --username USER    --password PASS .

 

 

 

 

转载于:https://www.cnblogs.com/xiaorong/archive/2008/09/05/1285014.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值