为自己用,写的一简单文件同步程序(vs2003调试通过)

using System;
using System.IO;
using System.Collections;
namespace ArkSyncCmd
{
    
/// <summary>
    
/// Class1 的摘要说明。
    
/// </summary>

    class ArkSync
    
{
        
static string fromPath;
        
static string toPath;
        
static string[] notPaths;
        
static ArrayList AllPaths;
        
static string[] notFiles;
        
static DateTime startTime;//运行时间
        static bool showMessge=true;
        
/// <summary>
        
/// 应用程序的主入口点。
        
/// </summary>

        [STAThread]
        
static void Main(string[] args)
        
{
            startTime
=DateTime.Now;
            System.Configuration.AppSettingsReader r
=new System.Configuration.AppSettingsReader();
            
if(args!=null&&args.Length==1&&args[0]=="0")showMessge=false;
            fromPath
=r.GetValue(("fromPath"),typeof(string)).ToString();
            toPath
=r.GetValue(("toPath"),typeof(string)).ToString();
            notPaths
=r.GetValue(("notPaths"),typeof(string)).ToString().Split(',');
            notFiles
=r.GetValue(("notFiles"),typeof(string)).ToString().Split(',');

            
string [] fromDir=Directory.GetDirectories(fromPath);//得到开始的目录

            
//Console.Write("开始目录\n");
            AllPaths=new ArrayList();
            AllPaths.Add(fromPath);
            GetAllPaths(fromDir,notPaths);
//得到所有的目录
            
//Console.Write("开始文件\n");
            ArrayList allFiles=GetAllFiles(AllPaths,notFiles);
            Console.Write(
"要更新的文件\n");
            ArrayList updateFiels
=GetUpDateFiles(allFiles);
            Console.Write(
""+updateFiels.Count+"条,时间:"+((System.TimeSpan)DateTime.Now.Subtract(startTime)).TotalSeconds+" s"+",按任意键继续\n");           
               Console.ReadLine();
            startTime
=DateTime.Now;
            CopyFiles(updateFiels);
            Console.Write(
"时间:"+((System.TimeSpan)DateTime.Now.Subtract(startTime)).TotalSeconds+" s,"+"更新完成\n");
            Console.ReadLine();
        }

        
static void CopyFiles(ArrayList updateFiles)
        
{            
            
string fail="";
            
foreach(string path in updateFiles)
            
{
                
string top=fromPathToPath(path,fromPath,toPath);
                
if(!Directory.Exists(Path.GetDirectoryName(top)))Directory.CreateDirectory(Path.GetDirectoryName(top));
                
if(showMessge)Console.Write("正在复制:"+path+"->"+top+"\n");
                
try
                
{
                    FileInfo fi
=new FileInfo(top);
                    
if( fi.Attributes.ToString().IndexOf("ReadOnly")!=-1 ) 
                        fi.Attributes
=FileAttributes.Normal; 
                    File.Copy(path,top,
true);
                }

                
catch(Exception ex)
                
{
                    
string ee=ex.ToString();
                    fail
+=path+"\n";
                }

            }

            Console.Write(
"失败的:\n"+fail);
        }

        
static ArrayList GetUpDateFiles(ArrayList AllFiles)
        
{
            DateTime datetime1;
            DateTime datetime2;
            ArrayList updateFiles
=new ArrayList();
            
foreach(string file in AllFiles)
            
{
                
string top=fromPathToPath(file,fromPath,toPath);
                
if(!File.Exists(top))
                
{
                    updateFiles.Add(file);
                    
if(showMessge)Console.Write(file+"-->"+top+"\n");
                    
continue;
                }

                
else
                
{
                    datetime1
=File.GetLastWriteTime(file);
                    datetime2
=File.GetLastWriteTime(top);
                    TimeSpan tsp
=datetime1-datetime2;
                    
if(Math.Abs(tsp.TotalSeconds)>1)
                    
{
                            updateFiles.Add(file);
                        
if(showMessge)Console.Write(file+"-->"+top+"\n");                                                    
                    }

                }

            }

            
return updateFiles;
        }

        
static string fromPathToPath(string path,string formPath,string toPath)
        
{
            
return path.Replace(formPath,toPath);
        }

        
static void GetAllPaths(string[] dirs,string[] notPs)
        
{
            
foreach(string dir in dirs)
            
{
                
if(StrIsIn(dir,notPs))continue;
                
string [] inDirs=Directory.GetDirectories(dir);
                GetAllPaths(inDirs,notPs);
                
//Console.Write(dir+"\n");
                AllPaths.Add(dir);
            }

        }

        
static ArrayList GetAllFiles(ArrayList allPaths,string []notFs)
        
{
            ArrayList allFiels
=new ArrayList();
            
foreach(string path in allPaths)
            
{
                
string [] files=Directory.GetFiles(path);
                
foreach(string file in files)
                
{
                    
if(StrIsIn(file,notFs))continue;
                    allFiels.Add(file);
                    
//Console.Write(file+"\n");
                }

            }

            
return allFiels;
        }

        
static bool StrIsIn(string checkVlues,string [] notPs)
        
{
            
foreach(string str1 in notPs)
                
if(checkVlues.EndsWith(str1))return true;
            
return false;
        }

    }

}

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
 
<add key="fromPath" value="E:\ark"/><!--原始目录-->
 
<add key="toPath"  value="E:\toark"/><!--镜像目录-->
 
<add key="notPaths" value="_svn,ark" /><!--不需要镜像的目录,多个以豆号分隔-->
 
<add key="notFiles" value="config,.cs" /><!--不需要镜像的文件,多个以豆号分隔-->
</appSettings>
</configuration>
写的比较简单,只为自己用,没考虑太多.
按任意键继续,没能实现,也没有去查资料,本想实现 按Esc键 退出,按其它键继续的.如有知道请告知.
posted @ 2006-05-11 22:37  吴乐章,炎刘中学  阅读(701)  评论(0编辑  收藏  举报