隐藏

C#创建目录

发布:2022/12/24 20:44:13作者:管理员 来源:本站 浏览次数:523

使用CreateDirectory方法创建目录。

假设您需要在D驱动器中创建目录。为此,请使用CreateDirectory()如下所示的方法-

Directory.CreateDirectory("D:\\Tutorial");

以下是代码-

示例

using System.IO; using System; public class Program {
   public static void Main() {
      Directory.CreateDirectory("D:\\Tutorial");
   }
}


            var strpatj = HttpRuntime.AppDomainAppPath;

            if (!Directory.Exists(strpatj + "\\temp"))

                Directory.CreateDirectory(strpatj + "\\temp");


            StreamReader sr = new StreamReader(filestrem, System.Text.Encoding.GetEncoding("gb2312")); //读取这个流

            using (StreamWriter sw = new StreamWriter(strpatj + "\\temp\\" + "temp.csv"))

            {

                sw.Write(sr.ReadToEnd()); //读取后, sr无数据

            }

            //带编码方式写入

            using (StreamWriter sw = new StreamWriter(strpatj + "\\temp\\" + "temp.csv", false, Encoding.GetEncoding("gb2312")))

            {

                sw.Write(sr0.ReadToEnd());

            }

            //在读取该文件,返回一个Stream.

            StreamReader sr1 = new StreamReader(strpatj + "\\temp\\" + "temp.csv", System.Text.Encoding.GetEncoding("gb2312")); //读取这个流

            filestrem = sr1.BaseStream;

            //未测试

            using (FileStream fsw = new FileStream(strpatj + "\\temp\\" + "temp.csv", FileMode.Create, FileAccess.Write))  //打开文件,用于只写??

            {

                BinaryWriter bw = new BinaryWriter(fsw); //编写器指向这个文件流??

                bw.Write(System.IO.File.ReadAllBytes(strpatj + "\\temp\\" + "temp.csv"));    //打开一个文件读取流信息,将其写入新文件??

                System.IO.File.Delete(path + i.ToString() + "_" + filename);        //删除指定文件信息??

                bw.Flush(); //清理缓冲区??

            }