文件复制 另存为 文件名修改

来源:互联网 发布:淘宝网店利润 编辑:程序博客网 时间:2024/05/13 06:51

文件复制:
toPath = NP_Global.PipeModelPath + model.Name + strExt;
 FileDirectory fd = new FileDirectory();
 fd.CopyFile(fromPath, toPath);

  public void CopyFile(string from, string to)
        {
            if (File.Exists(from))
            {
                File.Copy(from, to, true);
            }
            else
            {
                MessageBox.Show("该文件不存在","信息提示");
            }
        }

 

通过文件流另存为:
 public static void SaveModel(string file, byte[] fileData)//file 文件路径,fileData文件流
       {
           FileStream fs = new FileStream(file, FileMode.Create);
           BinaryWriter sw = new BinaryWriter(fs);
           //开始写入
           sw.Write(fileData);
           //清空缓冲区
           sw.Flush();
           //关闭流
           sw.Close();
           sw.Dispose();
           fs.Close();
           fs.Dispose();
       }

 

文件名修改:
 File.Move(path, newPath);

 

文件夹及文件复制:
 public static void CopyFolder(string from, string to)
        {

        if (!Directory.Exists(to))
                Directory.CreateDirectory(to);
            // 子文件夹
            foreach (string sub in Directory.GetDirectories(from))
            {
                var child = Path.Combine(to, Path.GetFileName(sub));
                CopyFolder(sub, child);
            }
            // 文件
            foreach (string file in Directory.GetFiles(from))
            {
                var f = Path.Combine(to, Path.GetFileName(file));
                File.Copy(file, f, true);
            }        
        }

0 0
原创粉丝点击