zoukankan      html  css  js  c++  java
  • C#文件和文件夹操作

    C#文件和文件夹操作

    http://www.csref.cn/vs100/method/System-IO-BinaryReader-ReadBytes.html

            string basePath = AppDomain.CurrentDomain.BaseDirectory + "\NewPhoneBillRecord\";
                //判断该路径下文件夹是否存在,不存在的情况下新建文件夹
                if (!Directory.Exists(basePath))
                {
                    Directory.CreateDirectory(basePath);
                }
                string postPath = basePath + DateTime.Now.ToString("yyyyMMddHHmmss") + ".txt";//路径+文件名        
                using (StreamWriter file = new StreamWriter(@postPath, false))
                {
                    file.WriteLine(lines);
                }
    
         StreamWriter 实现一个 TextWriter,使其以一种特定的编码向流知中写入字符
         例StreamWriter SWriter = new StreamWriter(path,append);
         表示-----
          path   要写入的完整文件路径。
         append  确定是道否将数据追加到文件。如果该文件存在,并回且 append 为 false,
                 则该文件被改写。如果该文件存在,并且 append 为 true,则数据被追加到该文件中。
                 否则,将创建新文答件
    
    
    C#清空txt内容,并追加内容
    1、将txt内容清空
    public void ClearTxt(String txtPath)  
            {  
                String appDir = System.AppDomain.CurrentDomain.BaseDirectory + @"Txt" + txtPath;  
                FileStream stream = File.Open(appDir, FileMode.OpenOrCreate, FileAccess.Write);  
                stream.Seek(0, SeekOrigin.Begin);  
                stream.SetLength(0);  
                stream.Close();  
            }
    
    2、向txt中追加内容
    public void SaveFile(string str,String txtPath,bool saOrAp) { 
               String appDir = System.AppDomain.CurrentDomain.BaseDirectory + @"Txt" + txtPath;  
               StreamWriter sw = new StreamWriter(appDir, saOrAp);//saOrAp表示覆盖或者是追加  
               sw.WriteLine(str);  
               sw.Close();  
           }
    
    3、c#创建目录和文件夹,数据写入并生成txt文件
    c#创建目录:
    
    // 获取程序的基目录。
    System.AppDomain.CurrentDomain.BaseDirectory
    
    // 获取模块的完整路径。
    System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName
    
    // 获取和设置当前目录(该进程从中启动的目录)的完全限定目录。
    System.Environment.CurrentDirectory
    
    // 获取应用程序的当前工作目录。
    System.IO.Directory.GetCurrentDirectory()
    
    // 获取和设置包括该应用程序的目录的名称。
    System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase
    
    // 获取启动了应用程序的可执行文件的路径。
    System.Windows.Forms.Application.StartupPath
    
    // 获取启动了应用程序的可执行文件的路径及文件名
    System.Windows.Forms.Application.ExecutablePath
    
    //组成新的路径
    
    string path=System.Windows.Forms.Application.StartupPath+"\DownFile\";
    
    //判断该路径下文件夹是否存在,不存在的情况下新建文件夹
    
    if(!Directory.Exists(path))
    
    {
    
    Directory.CreateDirectory(path);
    
    }
    
    //生成txt文件,将json字符串数据保存到txt文件
    
    string postPath=path+DateTime.Now.ToString("yyyyMMddHHmmss")+".txt";//路径+文件名
    
    byte[] bytes=null;
    
    bytes=Encoding.UTF8.GetBytes(Obj.ToString())//Obj为json数据
    
    FileStream fs=new FileStream(postPath,FileMode.Create);
    
    fs.Write(bytes,0,bytes.Length);
    
    fs.Close();
    
    
    将字符串写入文本
    public void writeTextToFile(string lines)
            {
    
                #region  将字符串数组写入到文件,每个元素为一行
                //string[] lines = { "First line", "Second line", "Third line" };
                //string textPath = Configuration.AppSetting();
                //File.WriteAllLines(@"C:UsersPublicTsetFolderWriteLines.txt", lines);
                #endregion
    
                #region 将字符串写入到文件
                //string text = "A class is the most powerful data type in C#. Like a structure, " + "a class defines the data and behavior of the data type. ";
    
                //File.WriteAllText(@"C:UsersPublicTestFolderWriteText.txt", text);
                #endregion
    
                #region  使用using语句动态写入到文件
                //using (StreamWriter file = new StreamWriter(@"C:UsersPublicTestFolderWriteLines2.txt"))
                //{
                //    foreach (string line in lines)
                //    {
                //        if (!line.Contains("Second"))
                //        {
                //            file.WriteLine(line);
                //        }
                //    }
                //}
                #endregion
    
                //获取程序的基目录并组成新路径
                string basePath = AppDomain.CurrentDomain.BaseDirectory + "NewPhoneBillRecord\";
                //判断该路径下文件夹是否存在,不存在的情况下新建文件夹
                if (!Directory.Exists(basePath))
                {
                    Directory.CreateDirectory(basePath);
                }
                string postPath = basePath + DateTime.Now.ToString("yyyyMMdd") + ".txt";//路径+文件名        
                using (StreamWriter file = new StreamWriter(@postPath, false)) //false:改写,true:追加
                {
                    file.WriteLine(lines);
                }
            }
    
    
  • 相关阅读:
    wpf 3D学习
    vs2010莫名崩溃初探
    Wcf(,service callback client)
    MEF和CompositionContainer学习
    认知Media Queries让浏览器人性化
    移动互联网产品设计的7大误区
    RUP中的迭代模型
    前端工程师的价值体现在哪里?
    做用户研究时,如何挑选合适的用户?
    晒晒 邀请函!感恩节 感谢组织!让我挡上末班车!哈哈!
  • 原文地址:https://www.cnblogs.com/newcapecjmc/p/12727316.html
Copyright © 2011-2022 走看看