震荡

     很久没这样写东西了,平时也很少用到IO方面的知识,所以,也没特意去研究,这几天闲着找点事做:由于源代码管理,在本地修改的页面要实时更新到服务器 上,所以,每次上传的时候都得一一找出哪些做了修改,很烦人,所以,就写了这个小应用程序用来查目录下某日期后修改过的所有文件和新增目录,今天刚写好, 很简单,可是在写的过程中也遇到一些很头痛疼的地方,也许还有不够细致的地方,贴出来,有兴趣的可以看下,希望对某部分朋友有所帮助,也希望老鸟们指点斧 正!

Form1:

Code System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.IO;

namespace MyForm
{
    
public partial class Form1 : Form
    
{
        
public Form1()
        
{
            InitializeComponent();
        }

        
private void btnLogin_Click(object sender, EventArgs e)
        
{
            
if (this.txtDate.Text == "")
            
{
                
this.lblResult.Text = "请输入目录地址";
                
return;
            }

            
if (this.txtPath.Text == "")
            
{
                
this.lblResult.Text = "请选择日期";
                
return;
            }

            StringBuilder builder 
= new StringBuilder();
            
try
            
{
                
string path = this.txtPath.Text.ToString();
                
int startPosition = path.LastIndexOf("""");
                
string r = Regex.Match(path, @"[""]([^""]+)""*$").Groups[1].Captures[0].Value;
                MyForm.FindOutNewFile findNew 
= new FindOutNewFile();


                DateTime d 
= Convert.ToDateTime(this.txtDate.Text);
                builder.Append(
@"E:"test"");
                builder.Append(d.ToShortDateString().Replace(
'-''_'));
                builder.Append(
"""" + r);
                
if (!Directory.Exists(builder.ToString()))
                
{
                    Directory.CreateDirectory(builder.ToString());
                }

                findNew.FindNew(path, 
new DirectoryInfo(builder.ToString()), "", d);
                
this.lblResult.Text = "有新修改的文件,现另存在目录:" + builder.ToString();

            }

            
catch (ArgumentOutOfRangeException)
            
{
                
this.lblResult.Text = "至少为一级目录!";
            }

            
catch (Exception r)
            
{
                
this.lblResult.Text = "failed:" + r.Message.ToString();
            }
           
        }


        
private void txtDate_Click(object sender, EventArgs e)
        
{
            
this.monthCalendar1.Visible = true;
        }


        
private void monthCalendar1_DateSelected(object sender, DateRangeEventArgs e)
        
{
            
this.txtDate.Text = this.monthCalendar1.SelectionStart.ToString();
            
this.monthCalendar1.Visible = false;
        }


        
private void Form1_Load(object sender, EventArgs e)
        
{

        }

    }

}

 


逻辑类:

Code System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
namespace MyForm
{
    
class FindOutNewFile
    
{
        
public FindOutNewFile()
        
{ }
        
/// <summary>
        
/// 查找某日以后修改过的文件,当新建的目录里面没有文件时将将创建
        
/// </summary>
        
/// <param name="parendPath">查找的源目录,至少是一级目录</param>
        
/// <param name="desParentDir">查找到的新文件存放目录</param>
        
/// <param name="splitPath">存放目录片段</param>
        
/// <param name="modifideDate">起始的日期</param>

        public void FindNew(string parendPath,DirectoryInfo desParentDir ,string splitPath, DateTime modifideDate)
        
{
            
if (!Directory.Exists(parendPath))
            
{
                
return;// "您所输入的路径不存在或输入有误,请重新输入!";
            }

            
else
            
{                
                DirectoryInfo dir
=new DirectoryInfo (parendPath);
                
//得到目录下的所有文件信息(FileSystemInfo:为FileInfo与DirectoryInfo的公共父类
                FileSystemInfo[] infos = dir.GetFileSystemInfos();
                
foreach(FileSystemInfo info in infos)
                
{
                    
//如果是文件
                    if (info is FileInfo)
                    
{

                        FileInfo file 
= info as FileInfo;
                        
//如果是新修改的文件
                        if (file.LastWriteTime >= modifideDate)
                        
{
                            
//得到目标目录的完整名
                            string filePath = desParentDir.FullName;
                            
//是否是目标根目录里
                            if (splitPath != "" && splitPath != null)
                            
{
                                
//这里创建子目录,目录片段  splitPath,不以“""”开头,而且它的头可以和pPath里的有重叠////////////
                                ///////这里很要注意,很容易让我们以“""”开头
                                DirectoryInfo pPath = desParentDir.CreateSubdirectory(splitPath);
                                filePath 
= pPath.FullName;
                            }

                            filePath 
+= """" + file.Name;
                            FileStream fs 
= new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite);
                            fs.Close();
                            
//新新修改的文件移到目标目录
                            file.CopyTo(filePath, true);
                            
return;
                        }

                    }

                    
else                        
                    
{
                        
//是目录,将目录名作加到片段里
                        DirectoryInfo dirHere = info as DirectoryInfo;
                        StringBuilder builder 
= new StringBuilder(dirHere.Name);
                        
//确保片段不是以“""”开头的
                        if (splitPath != "" && splitPath != null)
                        
{
                            builder.Append(
"""" + splitPath);
                        }

                        
//保证新目录里没有文件也将被创建
                        if (!Directory.Exists(desParentDir.FullName +""""+dirHere .Name ))
                        
{
                            desParentDir.CreateSubdirectory(dirHere.Name);
                        }

                        
//循环每个子目录
                        FindNew(dirHere.FullName, desParentDir, builder .ToString () , modifideDate);
                    }

                }

            }

        }

    }

}

posted @ 2009-03-30 23:44  Bluer  阅读(168)  评论(0编辑  收藏  举报