C#操作文件和文件夹的类

下面对处理文件常用的类做了一些介绍,还算详细吧,主要包括:DirectoryInfo,Directory,Path,FileInfo,File,FileStream。

private void button2_Click(object sender, EventArgs e)
        {           
            TimeSpan ts = new TimeSpan();
            DateTime dtStart = DateTime.Now;
            string currFile = textBox1.Text;
            string Json = File.ReadAllText(currFile).TrimStart();//将文件内容转换成字符串类型输出
            int index = Json.IndexOf('[');//判断某个字符在字符串里面的第几个位置
            #region DirectoryInfo 操作文件夹
            DirectoryInfo d = new DirectoryInfo(@"D:\ATS\Application\VS2017\Source Code"); d.Create();    \\创建目录:
            d.Delete();//删除目录:
            d.MoveTo(@"D:\ATS\Application\VS2017\Source Code");//移动目录:
            bool b = d.Exists; //目录是否存在:
            string fullname=d.FullName;//获得目录全名:
            FileInfo[] f = d.GetFiles();//获得子文件!对象信息!: 
            d.GetFiles("*.exe");//返回的是文件对象数组,内容更详细,只获取exe的文件
            DirectoryInfo[] dr = d.GetDirectories(); //获得子目录:
            #endregion DirectoryInfo 操作文件夹
            #region Directory操作文件夹
            Directory.CreateDirectory(@"D:\ATS\Application\VS2017\Source Code\Jay\oda1019\test\TEST");//创建文件夹
            Directory.Delete(@"D:\ATS\Application\VS2017\Source Code\Jay\oda1019\test\TEST", true);//删除TEST目录,true将删除整个目录,即使该目录下有文件或子目录;若为false,则仅当目录为空时才可删除。
            File.Move(@"c:\tempuploads\NewDirectory", @"c:\tempuploads\BackUp");//移动文件夹

            //下面的代码读出c:\tempuploads\目录下的所有子目录,并将其存储到字符串数组中。
            string[] Directorys;
            Directorys = Directory.GetDirectories(@"c:\tempuploads");

            //获取当前目录下的所有文件方法,下面的代码读出c:\tempuploads\目录下的所有文件,并将其存储到字符串数组中
            string[] Files;
            Files = Directory.GetFiles(@"c:\tempuploads");

            Directory.Exists(@"c:\tempuploads");    //判断目录是否存在
            Directory.GetDirectoryRoot(@"c:\tempuploads");//获取根目录

            Directory.GetParent(@"c:\tempuploads"); //获取上一级目录
            #endregion
            #region Path处理文件
            string file_name = Path.GetFileName(currFile);//从一个地址中解析出文件名
            Path.GetFileNameWithoutExtension(currFile);//从路径字符串中得到文件名(不带扩展名)
            Path.GetExtension(currFile);//从文件路径字符串中得到文件的扩展名
            Path.GetDirectoryName(currFile);//得到文件夹的路径
            Path.GetFullPath(currFile);//得到包括文件名和扩展名的全路径名。
            //合并路径
            string Str1 = @"C:\Users\Administrator\";
            string Str2 = @"Desktop\测试文件.txt";
            Path.Combine(Str1, Str2);
            #endregion
            #region FileInfo处理文件
            FileInfo fi = new FileInfo(currFile);
            DateTime dt = fi.CreationTime;  //获取或设置当前文件或目录的创建时间。 
            string directory = fi.DirectoryName;    //获取表示目录的完整路径的字符串
            bool IfExists = fi.Exists;  //获取指示文件是否存在
            string Extension = fi.Extension;    //获取表示文件扩展名部分的字符串
            string fullName = fi.FullName;  //获取目录或文件的完整目录
            string Name = fi.Name;  //获取文件名
            fi.CopyTo(@"D:\ATS\Application\VS2017\Source Code\Jay\oda1019\test\test.JSON", true);//将文件copy到指定路径,并且覆盖之前的文件
            fi.MoveTo(@"D:\ATS\Application\VS2017\Source Code\Jay\oda1019\test\test1.JSON");//将文件move到指定路径,并且覆盖之前的文件
            FileStream fs=fi.Create();  //创建一个文件
            fi.Delete();    //删除一个文件
            #endregion                            ////////////////////////////////////////////////////////////////////////////////
            #region File处理文件
            File.Create(@"D:\Test\Debug1\测试.txt");//创建文件
            File.Copy(@"c:\tempuploads\newFile.txt", @"c:\tempuploads\BackUp.txt", true);//copy文件        
            File.Delete(@"c:\tempuploads\newFile.txt");
            File.Move(@"C:\Users\scottzhang\NEW.txt", @"D:\ATS\Application\VS2017\Source Code\Jay\oda1019\test\NEW.txt");
            File.Exists(@"c:\tempuploads\newFile.txt");//判断文件是否存在
            #endregion
            #region FileStream读写文件,代码会报错
            //1.创建一个    中国.txt
            string txt = "中国是世界上人口第一大国。中国是世界上最幸福的国家之一。";
            //一、创建一个文件流
            FileStream fs1 = new FileStream(@"D:\ATS\Application\VS2017\Source Code\Jay\oda1019\test\NEW.txt", FileMode.Create, FileAccess.Write);
            byte[] buffer = Encoding.UTF8.GetBytes(txt);
            //二、读文件或者写文件
            //参数1:表示要把哪个byte[]数组中的内容写入到文件
            //参数2:表示要从该byte[]数组的第几个下标开始写入,一般都是0
            //参数3:要写入的字节的个数。
            fs.Write(buffer, 0, buffer.Length);
            ////三、关闭文件流
            ////清空缓冲区
            //fs.Flush();
            //fs.Close();
            //四、释放相关资源
            fs.Dispose();//自动调用close和flush方法
            Console.WriteLine("ok");
            Console.ReadKey();
            //1.创建一个    中国.txt
            string txt1 = "中国是世界上人口第一大国。中国是世界上最幸福的国家之一。中国是四大文明古国之一。中国有个杨中科。";
            //一、创建一个文件流
            //当把一个对象放到using()中的时候,当超出using的作用于范围后,会自动调用该对象的Dispose()f方法。
            using (FileStream fs2 = new FileStream(@"c:\中国.txt", FileMode.Create, FileAccess.Write))
            {
                byte[] buffer1 = Encoding.UTF8.GetBytes(txt);
                //二、读文件或者写文件
                //参数1:表示要把哪个byte[]数组中的内容写入到文件
                //参数2:表示要从该byte[]数组的第几个下标开始写入,一般都是0
                //参数3:要写入的字节的个数。
                fs.Write(buffer, 0, buffer.Length);
            }
            Console.WriteLine("ok");
            Console.ReadKey();
            #endregion

        }

原文链接:https://www.cnblogs.com/ziqiumeng/p/10537538.html

C#操作文件及文件夹的方式说明,主要用到的类:Directory,File。命名空间:using System.IO;

//1) 相对路径转绝对路径
string fullfolder = HttpContext.Current.Server.MapPath(folder);

//2) 文件移动(改名)
File.Move(Server.MapPath("/a.txt"), Server.MapPath("/b.txt"));

//3) 文件复制
File.Copy(Server.MapPath("/a.txt"), Server.MapPath("/b.txt"), true);

//4) 文件是否存在
File.Exists(filefullname)

//5) 目录是否存在
Directory.Exists(fullfolder))

//6) 创建目录
Directory.CreateDirectory(fullfolder);

//7) 目录移动
Directory.Move

//8) 读取文本文件
StreamReader srd = File.OpenText(fullfilename);
srd.ReadToEnd();
srd.Close();
srd.Dispose();

//9) 写文件
StreamWriter swr = File.CreateText(Server.MapPath("test.txt"));
swr.Write("message");
swr.Close();
swr.Dispose();

//10)删除文件
// 删除硬盘上的文件
if (File.Exists(filefullname))
{
File.Delete(filefullname);
}

//11)目录遍历
public void ListFiles(string pathname)
{
// 所有目录与文件
string[] subDirs = Directory.GetDirectories(pathname);
string[] subFiles = Directory.GetFiles(pathname);

foreach (string subDir in subDirs)
{
ListFiles(subDir);
}

// 所有文件
foreach (string subFile in subFiles)
{
string filename = Path.GetFileName(subFile);
}
}

//12)文件修改时间
FileInfo fi = new FileInfo(@"c:\test.txt");
DateTime writetime = fi.LastWriteTime;

//13)从含路径的文件名中提取文件名
System.IO.Path.GetFileName(fullPath);//文件名

原文链接:https://blog.csdn.net/fengqingtao2008/article/details/51519002

猜你喜欢

转载自www.cnblogs.com/Dumb-dog/p/12711139.html