C#IO

2021-03-05 10:28

阅读:445

标签:异常   tin   ===   efault   oid   current   static   datetime   垃圾   

//配置取KEY VAULE
private static string con = ConfigurationManager.ConnectionStrings["conn"].ToString(); //配置绝对的路径 private static string path = ConfigurationManager.AppSettings["FileFPath"]; private static string pathlog = ConfigurationManager.AppSettings["LogPath"]; //当前文件路径 private static string logpath = AppDomain.CurrentDomain.BaseDirectory;

--多的话你还去弄个类方便管理

---new 出来的文件夹对象一定要判断,不然你就没有那个文件他一样会有!!垃圾

创建

if (!Directory.Exists(path)) //文件帮助类 检查文件是不是存在
            {

            };
            DirectoryInfo dir = new DirectoryInfo(pathlog);    //文件夹对象 如果文件夹不存在他一样能new出来!!!!
            Console.WriteLine($"{dir.Exists},{dir.Extension},{dir.FullName}");
            //Console.ReadKey();



            if (!File.Exists(Path.Combine(logpath,"info.txt")))
            {

            }
            FileInfo fine = new FileInfo(Path.Combine(logpath, "info.txt"));
            Console.WriteLine($"{fine.FullName}{fine.CreationTime},{fine.LastWriteTime}");
            //Console.ReadKey();



            {
                //directory
                if (!Directory.Exists(logpath))
                {
                    DirectoryInfo dirinfo = Directory.CreateDirectory(logpath);//一次创建
                    //Directory.Move(logpath,LogMovePath);
                    //Directory.Delete(logpath);
                }
            }

//写入

  //
            {//fiile
                string filename = Path.Combine(logpath, "log.txt");
                string filenameCopy = Path.Combine(logpath, "logCopy.txt");
                string filenamemove = Path.Combine(logpath, "LogMove");

                bool isexits = File.Exists(filename);
                if (!isexits)
                {//写入
                    //如果不存在就创建
                    Directory.CreateDirectory(logpath);
                    using (FileStream filestr=File.Create(filename)) //打开文件流写入
                    {
                        string name = "4545454";
                        byte[] byts = Encoding.Default.GetBytes(name);
                        filestr.Write(byts, 0, byts.Length);
                        filestr.Flush();
                    }
                    //using (FileStream fs=File.Create(filename))
                    //{
                    //    StreamWriter sw = new StreamWriter(filename);
                    //    sw.WriteLine("124343433");
                    //    sw.Flush();
                    //}
                    using (StreamWriter sw=File.AppendText(filename))
                    {
                        string msg = "sdsdsdasdsdada";
                        sw.WriteLine(msg);
                        sw.Flush();
                    }

                    using (StreamWriter sw = File.AppendText(filename))
                    {
                        string msg = "sdsdsdasdsdada";
                        byte[] bty = Encoding.Default.GetBytes(msg);
                        sw.BaseStream.Write(bty, 0, bty.Length);
                        sw.Flush();
                    }
                }

//读

   //
                {
                    foreach (string result in File.ReadAllLines(filename))
                    {
                        Console.WriteLine(result);
                    }
                    string sresult = File.ReadAllText(filename);
                    byte[] bytes = File.ReadAllBytes(filename);
                    string sreulstbytes = Encoding.UTF8.GetString(bytes);

                    using (FileStream strem = File.OpenRead(filename))
                    {
                        int length = 5;//1024好些
                        int result = 0;

                        do
                        {
                            byte[] btyes = new byte[length];
                            result = strem.Read(btyes, 0, 5);
                            for (int i = 0; i )
                            {
                                Console.WriteLine(bytes[i].ToString());
                            }
                        } while (length==result);
                    }

                    File.Copy(filename, filenameCopy);
                    File.Move(filename,filenamemove);
                    File.Delete(filenameCopy);
                    File.Delete(filenamemove);
                }

            }

方法解析

      {//方法解析Path
            Console.WriteLine(Path.GetDirectoryName(logpath));//要看会末尾的返回值对结果有影响
            Console.WriteLine(Path.GetDirectoryName(@"D\\abd"));//返回的是d:\
            Console.WriteLine(Path.GetDirectoryName(@"D\\abd\"));//返回的是d:\
            Console.WriteLine(Path.GetRandomFileName());//随机
            Console.WriteLine(Path.GetFileNameWithoutExtension("d:\\abd.txt"));//只返回文件名abd
            Console.WriteLine(Path.GetInvalidPathChars());//禁止在路径上使用字符
            Console.WriteLine(Path.GetInvalidFileNameChars());//禁止在路径上使用字符
            Console.WriteLine(Path.Combine(logpath,""));//h合并
            }
        }

///例子  千万别把程序的异常抛出来 ,你会很爽

   public static void logshow(string msg)
        {
            StreamWriter sw = null; ;
            try
            {
                string filename = "log.txt";
                string tatalpath = Path.Combine(pathlog,filename);
                if (!Directory.Exists(pathlog))
                {
                    Directory.CreateDirectory(pathlog);
                }
                sw = File.AppendText(pathlog);
                sw.WriteLine($"{DateTime.Now},{msg}");
                sw.WriteLine("====================================");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                
            }
            finally
            {
                if (sw != null)
                {
                    sw.Flush();
                    sw.Close();
                    sw.Dispose();
                }
            }
        }

 

  

 

C#IO

标签:异常   tin   ===   efault   oid   current   static   datetime   垃圾   

原文地址:https://www.cnblogs.com/yijieyufu/p/12901792.html


评论


亲,登录后才可以留言!