zoukankan      html  css  js  c++  java
  • C#判断文件是否存在 //创建txt文件

     if(System.IO.File.Exists(@""))
                {
        
                }
    
                if (System.IO.File.Exists(HttpRuntime.AppDomainAppPath + model.FilePath)) //判断文件是否存在
                {
                    try
                    {
                        System.IO.File.Delete(HttpRuntime.AppDomainAppPath + model.FilePath);
                        //删除表中记录
         
                        db.SaveChanges();
                    }
                    catch (System.IO.IOException e)
                    {
    
                    }
                }
     var path =  Path.GetDirectoryName(filePath); 获得文件路径
    
    
    var dir = path + "/" + docdeta[i].Column8.Substring(0, docdeta[i].Column8.LastIndexOf('.'))+ "_files";
    FileAttributes attr = System.IO.File.GetAttributes(dir); //经过测试目录不存在报异常,
    if (attr == FileAttributes.Directory)
    {
    Directory.Delete(path, true); //删除目录
    }
    
    if (Directory.Exists(dir))//判断是否存在 (用这个)
    {
    Directory.Delete(path, true);
    }
     private void button1_Click(object sender, EventArgs e)
            {
                SaveTxt(@"C:新建文件夹123.txt");
    
            }
            public void SaveTxt(string path)
            {
    
                #region --判断目录是否存在
                //当目录是@"C:新建文件夹123.txt" 创建123.txt文件夹
                if (Directory.Exists(path) == false)//如果不存在就创建file文件夹
                {
                    Directory.CreateDirectory(path);
                }
                #endregion
    
                #region --如果文件存在,将覆盖文件
                //创建文件, 如果文件已存在,将被覆盖
                FileStream fs1 = new FileStream(path, FileMode.Create, FileAccess.Write);
                StreamWriter sw0 = new StreamWriter(fs1);
                sw0.WriteLine("123456");//开始写入值
                sw0.Close(); sw0.Dispose();
                fs1.Close(); fs1.Dispose();
                #endregion
    
    
                #region --文件不存创建,存在则追加
                //文件不存在则创建,存在则追加追加内容
                FileStream fs = new FileStream(path, FileMode.Append, FileAccess.Write);
                StreamWriter sr = new StreamWriter(fs);
                sr.WriteLine("0000000");//开始写入值
                sr.Close(); sr.Dispose();
                fs.Close(); fs.Dispose();
                #endregion
    
                #region --如果文件存在,将覆盖文件
                StreamWriter sw = new StreamWriter(path);
                sw.WriteLine("211321456");
                sw.Flush(); //
                sw.Close(); sw.Dispose();
                #endregion
    
    
                System.Diagnostics.Process.Start("explorer.exe", path.Substring(0, path.LastIndexOf("\") + 1));
    
            }
  • 相关阅读:
    二维数组
    快速排序
    冒泡排序2
    对char类型数组的英文字母进行冒泡排序
    对char类型的数组进行冒泡排序
    冒泡排序
    对数组随机赋值,并输出(Arrays.toString(arr))
    数组声明的几种方式以及length属性
    猜拳游戏二
    二维小波包重构wprec2wprcoef
  • 原文地址:https://www.cnblogs.com/enych/p/9435156.html
Copyright © 2011-2022 走看看