下载中心┊文章中心┊软件分类┊更新软件┊推荐下载┊热门软件┊更新文章┊热门文章
 
《拓文asp.net网站内容管理系统》官方演示站。
 设为首页    
 收藏本站
 作者:黄振安
 
网站首页 开源拓文 .NET编程 网站编程 网页设计 图形图像 数据库 软件工程留言本
    站内搜索:   
您现在的位置: 拓文网络 → 文章中心 → .NET编程 → C#.NET → 文件的一些基本操作[正文]

热 门 文 章

相 关 文 章
  • C#常用函数和方法集汇总
  • c#中分割字符串的几种方法(sp...
  • C#调用父类的父类的方法
  • 计算俩个时间之间隔多少月
  • 批量字符串替换程序-1
  • 批量字符串替换程序-2
  • C#中利用SharpZipLib进行文件...
  • C#快速排序类
  • c#学习体会:使用 ref 和 out ...
  • C#.NET 中的类型转换
  • 文件的一些基本操作
     
    [作者:不详    转贴自:不详    点击数:    字体:【大 中 小】    更新时间:2006-3-16 10:11:48]

    using System;
    using System.IO;
    using System.Text;

    namespace Document.Bll
    {
        /**//// <summary>
        /// Summary description for fileinfo.
        /// </summary>
        public class fileinfo
        {
            public fileinfo()
            {
                //
                // TODO: Add constructor logic here
                //
            }
           
           
            获取某目录下的所有文件(包括子目录下文件)的数量#region 获取某目录下的所有文件(包括子目录下文件)的数量       
            public int GetFileNum(string Path)
            {
                int fileNum = 0;
                string[] fileList = System.IO.Directory.GetFileSystemEntries(Path);
                // 遍历所有的文件和目录
                foreach(string file in fileList)
                {
                    if(System.IO.Directory.Exists(file))
                        GetFileNum(file);
                    else
                        fileNum++;
                }           
                return fileNum;
            }
            #endregion

            获取某目录下的所有文件(包括子目录下文件)的大小#region 获取某目录下的所有文件(包括子目录下文件)的大小
            public long GetDirectoryLength(string dirPath)
            {
                if(!Directory.Exists(dirPath))
                    return 0;
                long len=0;
                DirectoryInfo di=new DirectoryInfo(dirPath);
                foreach(FileInfo fi in di.GetFiles())
                {
                    len+=fi.Length;
                }
                DirectoryInfo[] dis=di.GetDirectories();
                if(dis.Length>0)
                {
                    for(int i=0;i<dis.Length;i++)
                    {
                        len+=GetDirectoryLength(dis[i].FullName);
                    }
                }
                return len;
            }
            #endregion

            读取文件#region 读取文件
            private string file_get_contents(string path)
            {
                StringBuilder s=new StringBuilder();
                using (StreamReader sr = new StreamReader(path,System.Text.Encoding.GetEncoding ("GB2312")))
                {
                    while (sr.ReadLine()!= null && sr.ReadLine().ToString()!="")
                    {
                        s.Append(sr.ReadLine());
                    }
                }           
                return s.ToString();
            }
            #endregion

            写文件#region 写文件
            public static void writefile()
            {
                StreamWriter sw = new StreamWriter("TestFile.txt",true,System.Text.Encoding.GetEncoding("GB2312")) ;
                // Add some text to the file.
                sw.Write("This is the ");
                sw.WriteLine("header for the file.");
                sw.WriteLine("-------------------");
                // Arbitrary objects can also be written to the file.
                sw.Write("The date is: ");
                sw.WriteLine(DateTime.Now);
            }
            #endregion

            System.IO.Path#region System.IO.Path
            private void System_IO_Path()
            {
                string path=@"c:\test\a.txt";

                string ChangeExtension=System.IO.Path.ChangeExtension(path,".old");//更改路径字符串的扩展名。
                string CombinePath=System.IO.Path.Combine(@"c:\","b.txt");//合并两个路径字符串。
                string DirectoryName=System.IO.Path.GetDirectoryName(path);//返回指定路径字符串的目录信息。
                string Extension=System.IO.Path.GetExtension(path);//返回指定的路径字符串的扩展名。
                string FileName=System.IO.Path.GetFileName(path);//返回指定路径字符串的文件名和扩展名。
                string FileNameWithoutExtension=System.IO.Path.GetFileNameWithoutExtension(path); //返回不具有扩展名的指定路径字符串的文件名。
                string FullPath=System.IO.Path.GetFullPath(path);//返回指定路径字符串的绝对路径。
                string PathRoot=System.IO.Path.GetPathRoot(path);//获取指定路径的根目录信息。
                string TempFileName=System.IO.Path.GetTempFileName();//返回唯一临时文件名并在磁盘上通过该名称创建零字节文件。
                string TempPath=System.IO.Path.GetTempPath();//返回当前系统的临时文件夹的路径。
                string HasExtension=System.IO.Path.HasExtension(path).ToString();//确定路径是否包括文件扩展名。
                string IsPathRooted=System.IO.Path.IsPathRooted(path).ToString();//获取一个值,该值指示指定的路径字符串是包含绝对路径信息还是包含相对路径信息。
            }

            #endregion
        }
    }


    页次:1/1  1篇/页  首页   上一页   下一页   尾页  合计 1 篇 Go:
     
    上一篇:如何将数据从c#导出到excel表
    下一篇:用C#快速往Excel写数据
    winner 打印本页 关闭窗口

         文 章 评 论
    评论人 查看该文章的评论 发表时间
       笔 名* : 0 50分 60分 70分 80分 90分 100分
    评论内容 *: (最多600字。当前字数:0)


    没有相关评论,欢迎您参与!

    - 关于本站 - 网站帮助 - 下载声明 - 友情连接 - 网站地图 -
    Copyright © 2005-2006 myTopWin.Com. All Rights Reserved . [桂ICP备06004496号]