码迷,mamicode.com
首页 > Windows程序 > 详细

C#常用IO操作

时间:2019-12-24 13:23:02      阅读:110      评论:0      收藏:0      [点我收藏+]

标签:public   utf-8   exist   create   递归   memory   main   opened   indexof   

文件夹

创建文件夹

//如果文件夹路径不存在则创建文件夹
if (!Directory.Exists(path))
    Directory.CreateDirectory(path);

递归创建文件夹

技术图片
        public void createdir(string fullpath)
        {
            if (!File.Exists(fullpath))
            {
                string dirpath = fullpath.Substring(0, fullpath.LastIndexOf(\\));
                //string[] pathes = dirpath.Split(‘\\‘);
                string[] pathes = fullpath.Split(\\);
                if (pathes.Length > 1)
                {
                    string path = pathes[0];
                    for (int i = 1; i < pathes.Length; i++)
                    {
                        path += "\\" + pathes[i];
                        //如果文件夹路径不存在则创建文件夹
                        if (!Directory.Exists(path))
                            Directory.CreateDirectory(path);
                    }
                }
            }
        }
View Code

 

文件

 

创建文件

            //path是完整路径,要包含文件的后缀名
            string path = @"C:\1.txt";
            //判断文件是否存在,不存在就创建
            if (!File.Exists(path))
            {

                //创建一个 UTF-8 编码text文件
                File.CreateText(path);


                //创建一个文件
                //File.Create(path);
            }

 写入文件

using System.IO;

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            //写入文本,不是追加,是清空在写入
            string path = @"C:\1.txt";
            string[] lines = { "First line", "Second line", "Third line" };
            File.WriteAllLines(path, lines);
            File.WriteAllText(path, "AAAAAAAAAAAAAAAA");

            //追加写入文本
            using (StreamWriter sw = new StreamWriter(path, true))
            {
                sw.WriteLine("Fourth line");
            }
            using (StreamWriter sw = File.AppendText(path))
            {
                sw.Write(12345);
            }

        }
    }
}

 

文件流读写

        public Stream FileToStream(string fileFullName)
        {
            using (FileStream fs = new FileStream(fileFullName, FileMode.OpenOrCreate))
            {
                byte[] bytes = new byte[fs.Length];
                fs.Read(bytes, 0, bytes.Length);
                fs.Close();
                return new MemoryStream(bytes);
            }
        }

        public void WriteFile(string fileFullName, byte[] bytes)
        {
            if (bytes == null)
                return;
            using (FileStream fs = new FileStream(fileFullName, FileMode.OpenOrCreate))
            {
                fs.Write(bytes, 0, bytes.Length);
                fs.Close();
            }

            #region 第二种
            /*
                using (MemoryStream m = new MemoryStream(bytes))
                using (FileStream fs = new FileStream(fileFullName, FileMode.OpenOrCreate))
                {
                    m.WriteTo(fs);
                }
             */
            #endregion

        }

 

 

Stream 和 byte[] 互转

        /// <summary>
        /// 将 Stream 转成 byte[]
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public byte[] StreamToBytes(Stream stream)
        {
            byte[] bytes = new byte[stream.Length];
            stream.Read(bytes, 0, bytes.Length);
            stream.Seek(0, SeekOrigin.Begin);
            stream.Flush();
            stream.Close();
            return bytes;

        }

        /// <summary>
        /// 将 byte[] 转成 Stream
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public Stream BytesToStream(byte[] bytes)
        {
            return new MemoryStream(bytes);
        }

C#常用IO操作

标签:public   utf-8   exist   create   递归   memory   main   opened   indexof   

原文地址:https://www.cnblogs.com/RainFate/p/12090798.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!