| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1099 人关注过本帖
标题:C#文件操作
只看楼主 加入收藏
落羽飘寻
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2010-2-17
结帖率:100%
收藏
已结贴  问题点数:10 回复次数:11 
C#文件操作
我想写一个程序,用到app.setting,使得文件夹里面没有数据的自动删除,最后一次修改与现在时间差10天的自动删除,请赐教谢谢~~~

搜索更多相关主题的帖子: 文件 
2010-02-17 16:08
athenalux
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:河北石家庄
等 级:小飞侠
威 望:8
帖 子:975
专家分:2514
注 册:2008-11-26
收藏
得分:0 
说的一点都不详细,不知道到底要干什么

QQ:81704464
2010-02-17 16:50
落羽飘寻
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2010-2-17
收藏
得分:0 
就是相当一个自动删除的工具 ,文件夹里面没东西就删掉,最后一次访问时间超过多少就删掉,现在部分问题解决了,如下问题是:
1、怎么配置app。setting,没有用到啊感觉,但是上面要求用到这个
2、我要是有一个文件夹的子文件夹有内容怎么判断,要是文件在最深一层,那查起来太麻烦了吧,谢谢


代码:
using System;
using System.Collections.Generic;
using System.Text;
using

namespace timespans
{
    class Program
    {
        static void Main(string[] args)
        {
            DirectoryInfo di = new DirectoryInfo("F:\\win03\\");
            // Get a reference to each file in that directory.
            FileInfo[] fiArr = di.GetFiles();
            DirectoryInfo[] diArr =di.GetDirectories();
            // Display the names of the files.
            foreach (FileInfo fri in fiArr)
            {
                DateTime DateTime2 = DateTime.Now;//现在时间  
                DateTime DateTime1 = fri.LastWriteTime;
                //DateTime1 = Convert.ToDateTime(); //设置要求的减的时间              
                string dateDiff = null;
                TimeSpan ts1 = new TimeSpan(DateTime1.Ticks);
                TimeSpan ts2 = new TimeSpan(DateTime2.Ticks);
                TimeSpan ts = ts1.Subtract(ts2).Duration();              //显示时间            
                dateDiff = ts.Days.ToString() + "天" + ts.Hours.ToString() + "小时" + ts.Minutes.ToString() + "分钟" + ts.Seconds.ToString() + "秒";
                Console.WriteLine(dateDiff);
                Console.WriteLine(fri.Name);

                if (ts.Days > 10)
                {
                    Console.WriteLine("timeout!,it has been deleted");
                    fri.Delete();
                }
            }
            foreach (DirectoryInfo dri in diArr)
            {
                Console.WriteLine(dri.Name);
            }

            int count = di.GetFiles().Length;
            int count2 = di.GetDirectories().Length;
            if (count == 0 && count2 == 0)
            {
                Console.WriteLine("no files and folders! it has been deleted");
                di.Delete();
            }

            Console.WriteLine("files: " + count);
            Console.WriteLine("folder: " + count2);
        }
    }
}
2010-02-17 17:26
~栀子花~
Rank: 6Rank: 6
来 自:~无泪之城~
等 级:侠之大者
威 望:3
帖 子:60
专家分:459
注 册:2010-2-12
收藏
得分:0 
我也不太懂

我允许你走进我的世界,但不许在我的世界里走来走去
2010-02-18 11:01
saitor
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
威 望:5
帖 子:373
专家分:1520
注 册:2009-5-18
收藏
得分:0 
1.不知道为什么要用app。setting难道要配置文件类型,不能写死类型?
2.递归。
2010-02-18 15:40
qubo1982
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:1
帖 子:367
专家分:1132
注 册:2009-3-18
收藏
得分:10 
删空文件夹,打完没试过,仅供参考
        bool CheckFolderIsNull(DirectoryInfo di)
        {
            DirectoryInfo[] dis = di.GetDirectories();
            if (dis.Length != 0)
            {
                foreach (DirectoryInfo d in dis)
                {
                    if (!CheckFolderIsNull(d))
                    {
                        return false;
                    }
                }
            }
            FileInfo[] fis = di.GetFiles();
            if (fis.Length > 0)
            {
                return false;
            }
            else
            {
                di.Delete();
                return true;
            }
        }
2010-02-19 16:40
qubo1982
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:1
帖 子:367
专家分:1132
注 册:2009-3-18
收藏
得分:0 
你也可以把删文件添在里面
            FileInfo[] fis = di.GetFiles();

            //删除文件代码 ...//

            fis = di.GetFiles();

            if (fis.Length > 0)
            {
                return false;
            }
            else
            {
                di.Delete();
                return true;
            }
2010-02-19 16:42
落羽飘寻
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2010-2-17
收藏
得分:0 
using System;
using System.Collections.Generic;
using System.Text;
using

namespace timespans
{
    class Program
    {

     static bool CheckFolderIsNull(DirectoryInfo di)
        {
            // Get a reference to each file in that directory.
            FileInfo[] fiArr = di.GetFiles("F:\\win03\\");
            // Get a reference to each subdirectory in that directory.
            DirectoryInfo[] diArr = di.GetDirectories("F:\\win03\\");
            DateTime DateTime2;
            DateTime DateTime1;   
            string dateDiff = null;
            TimeSpan ts;
            foreach (FileInfo fri in fiArr)
            {

                DateTime2 = DateTime.Now;//现在时间  
                DateTime1 = fri.LastWriteTime;
                TimeSpan ts1 = new TimeSpan(DateTime1.Ticks);
                TimeSpan ts2 = new TimeSpan(DateTime2.Ticks);
                ts = ts1.Subtract(ts2).Duration();              //显示时间  
                //DateTime1 = Convert.ToDateTime(); //设置要求的减的时间              

                dateDiff = "该文件距离上次访问时间相差:" + ts.Days.ToString() + "天" + ts.Hours.ToString() + "小时" + ts.Minutes.ToString() + "分钟" + ts.Seconds.ToString() + "秒";
                Console.WriteLine(fri.Name + " " + dateDiff);

                if (diArr.Length != 0 || ts.Days <= 10)
                {
                    foreach (DirectoryInfo d in diArr)
                    {
                        if (!CheckFolderIsNull(d))
                        {
                            return false;
                        }
                    }
                }
              
                if (fiArr.Length > 0 || ts.Days <= 10)
                {
                    return false;
                }
                else
                {
                    di.Delete();
                    Console.WriteLine("已经清理多余文件!");
                    return true;
                }
            }
        }


        static void Main(string[] args)
        {
            int count = di.GetFiles().Length;
            int count2 = di.GetDirectories().Length;
            DirectoryInfo di;

            Console.WriteLine("before files: " + count);
            Console.WriteLine("before folder: " + count2);
            
            
            CheckFolderIsNull(di);


            Console.WriteLine("after files: " + count);
            Console.WriteLine("after folder: " + count2);
        }

    }
}
2010-02-20 14:08
落羽飘寻
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2010-2-17
收藏
得分:0 
添加了递归函数的调用 还是没出来 望高手赐教(更正)。
2010-02-20 14:10
qubo1982
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:1
帖 子:367
专家分:1132
注 册:2009-3-18
收藏
得分:0 
DirectoryInfo[] diArr = di.GetDirectories("F:\\win03\\");
递归里面你给了一个定值,它还要怎么递归呢,每次递归都在  "F:\\win03\\" 
2010-02-20 14:14
快速回复:C#文件操作
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017652 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved