C# Directory和File

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/tran119/article/details/82805493

http://www.cnblogs.com/lgx5/p/6890641.html C# Directory类的操作
System.IO.Directory类和System.DirectoryInfo类的相关操作

public void CreatDir(string path)
{
    Directory.CreateDirectory(path);//是否存文件夹,存在就直接返回,不创建文件夹
}
public void SetDirInfo(string path) 
{
    DirectoryInfo info = new DirectoryInfo(path);//根据文件路径拿到文件夹的信息,设置文件夹信息
    info.Attributes = FileAttributes.ReadOnly | FileAttributes.Hidden;//如果为只读的目录是不允许删除的
}
public void DelectDir(string path,bool delectNotEmpty)
{
    Directory.Delete(path, delectNotEmpty);//第2个参数为ture则直接删除目录,为false则判断是否是空目录,空目录直接删除,非空目录就报错。
}
public void MoveDir(string sourceDir, string destDir)
{
    Directory.Move(sourceDir,destDir);//将文件夹从一个目录移动到另一个目录,且可以修改文件夹的名字,可以操作只读文件夹。只能在同一个盘操作。
}
public void GetDirChilds(string path)
{
    string[] childs = Directory.GetDirectories(path);//@"F:\"表示F盘下所有的文件夹,打印为全部路径,即F:\xxx
    for (int i = 0; i < childs.Length; i++)
    {
        print("childs name "+childs[i]);
    }
}
public void GetDirFiles(string path)
{
    string[] files = Directory.GetFiles(path);//拿到文件夹下的所有文件的路径名字,即F:\xxx
    for (int i = 0; i < files.Length; i++)
    {
        print("files name "+files[i]);
    }
}
public bool DirExist(string path) 
{
    return Directory.Exists(path);//该路径的文件夹是否存在
}

https://www.cnblogs.com/ldyblogs/p/file.html C# File类的操作
System.IO.File类和System.IO.FileInfo类主要提供有关文件的各种操作
下面的方法主要对UTF-8的编码文本进行操作,从而显得不够灵活。在这里推荐读者使用下面的代码对txt文件进行操作。
StreamReader TxtReader = new StreamReader(@"c:\tempuploads\newFile.txt",System.Text.Encoding.Default);
StreamWriter = new StreamWrite(@"c:\tempuploads\newFile.txt",System.Text.Encoding.Default);

public void openFile(string path,FileMode mode)
{
    FileStream stream = File.Open(path, mode);//Append模式如果不存在文件就创建新的文件
    byte[] content = new byte[] { (byte)'h',(byte)'i'};
    stream.Write(content, 0, content.Length);
}
public void creatFile(string path)
{
    /*由于File.Create方法默认向所有用户授予对新文件的完全读/写访问权限,所以文件是用读/写访问权限打开的,
     * 必须关闭后才能由其他应用程序打开。为此,所以需要使用FileStream类的Close方法将所创建的文件关闭。
     如果文件存在就先删除文件,再创建*/
    FileStream stream = File.Create(path);
    byte[] content = new byte[] { (byte)'h', (byte)'i' };
    stream.Write(content, (int)stream.Length, (int)stream.Length + content.Length);
    stream.Close();//必须关闭,否则程序中无法openFile打开操作该文件。
}
public void deleteFile(string path)
{
    File.Delete(path);
}
public void copyFile(string sourceFilePath,string destFilePath,bool overwrite)
{
    //1,不存在文件就直接报错,2,如果overwrite为false,destFilePath文件存在就直接报错,不存在就复制一份,
    //3,overwrite为true,不论destFilePath文件是否存在,直接覆盖内容。
    File.Copy(sourceFilePath,destFilePath,overwrite);
}
public void moveFile(string sourceFilePath,string destFilePath)
{
    File.Move(sourceFilePath,destFilePath);//destFilePath文件存在就报错
}
public void setFileAttr(string path,FileAttributes attr)
{
    File.SetAttributes(path,attr);//设置该路径的文件的属性
}
public bool fileExist(string path)
{
    return File.Exists(path);//文件是否存在
}
 

猜你喜欢

转载自blog.csdn.net/tran119/article/details/82805493