zoukankan      html  css  js  c++  java
  • 为自己用,写的一简单文件同步程序(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键 退出,按其它键继续的.如有知道请告知.
  • 相关阅读:
    【vijos】【优先队列】合并果子
    【vijos】【二叉树】FBI树
    【NOIp复习】数据结构之栈、队列和二叉树
    【Leetcode】53. Maximum Subarray
    PHP json_encode转换空数组为对象
    206. Reverse Linked List
    151. Reverse Words in a String
    74. Search a 2D Matrix
    557. Reverse Words in a String III
    【Leetcode】79. Word Search
  • 原文地址:https://www.cnblogs.com/ark/p/397767.html
Copyright © 2011-2022 走看看