文件的一些基本操作

using  System;
None.gif
using  System.IO;
None.gif
using  System.Text;
None.gif
None.gif
namespace  Document.Bll
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Summary description for fileinfo.
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class fileinfo
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public fileinfo()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// TODO: Add constructor logic here
InBlock.gif            
//
ExpandedSubBlockEnd.gif
        }

InBlock.gif        
InBlock.gif        
ContractedSubBlock.gifExpandedSubBlockStart.gif        
获取某目录下的所有文件(包括子目录下文件)的数量#region 获取某目录下的所有文件(包括子目录下文件)的数量        
InBlock.gif        
public int GetFileNum(string Path)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
int fileNum = 0;
InBlock.gif            
string[] fileList = System.IO.Directory.GetFileSystemEntries(Path);
InBlock.gif            
// 遍历所有的文件和目录
InBlock.gif
            foreach(string file in fileList)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if(System.IO.Directory.Exists(file))
InBlock.gif                    GetFileNum(file);
InBlock.gif                
else
InBlock.gif                    fileNum
++;
ExpandedSubBlockEnd.gif            }
            
InBlock.gif            
return fileNum;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
获取某目录下的所有文件(包括子目录下文件)的大小#region 获取某目录下的所有文件(包括子目录下文件)的大小
InBlock.gif        
public long GetDirectoryLength(string dirPath)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(!Directory.Exists(dirPath))
InBlock.gif                
return 0;
InBlock.gif            
long len=0;
InBlock.gif            DirectoryInfo di
=new DirectoryInfo(dirPath);
InBlock.gif            
foreach(FileInfo fi in di.GetFiles())
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                len
+=fi.Length;
ExpandedSubBlockEnd.gif            }

InBlock.gif            DirectoryInfo[] dis
=di.GetDirectories();
InBlock.gif            
if(dis.Length>0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
for(int i=0;i<dis.Length;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    len
+=GetDirectoryLength(dis[i].FullName);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
return len;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
读取文件#region 读取文件
InBlock.gif        
private string file_get_contents(string path)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            StringBuilder s
=new StringBuilder();
InBlock.gif            
using (StreamReader sr = new StreamReader(path,System.Text.Encoding.GetEncoding ("GB2312"))) 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                       
string line;
                            while ((line = sr.ReadLine()) != null)

ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    s.Append(line);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }
            
InBlock.gif            
return s.ToString();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

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

ExpandedSubBlockEnd.gif        
#endregion

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

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/cheatlove/articles/405381.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值