为了正常的体验网站,请在浏览器设置里面开启Javascript功能!

Console程序移动文件

2018-04-29 4页 doc 15KB 10阅读

用户头像

is_180829

暂无简介

举报
Console程序移动文件Console程序移动文件 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; namespace Move_file { class Program { static void Main(string[] args) { string strFromPath = @"E:\From_File"; string strToPath =@"E:\To_...
Console程序移动文件
Console程序移动文件 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; namespace Move_file { class Program { static void Main(string[] args) { string strFromPath = @"E:\From_File"; string strToPath =@"E:\To_File"; MoveFolder(strFromPath, strToPath); } public static void MoveFolder(string strFromPath, string strToPath) { //如果源文件夹不存在,则创建 if (!Directory.Exists(strFromPath)) { Directory.CreateDirectory(strFromPath); } //创建数组保存源文件夹下的文件名 string[] strFiles = Directory.GetFiles(strFromPath); //循环移动文件 for (int i = 0; i < strFiles.Length; i++) { //取得移动的文件名,只取文件名,地址截掉。 string strFileName = strFiles[i].Substring(strFiles[i].LastIndexOf("\\") + 1, strFiles[i].Length - strFiles[i].LastIndexOf("\\") - 1); //开始移动文件,如果已存在,则覆盖 string toFile = strFromPath + "\\" + strFileName; if (!IsFileInUse(toFile)) { File.Copy(strFiles[i], strToPath + "\\" + strFileName,true); if (File.Exists(toFile)) { FileInfo fi = new FileInfo(strFileName); if (fi.Attributes.ToString().IndexOf("ReadOnly") != -1) fi.Attributes = FileAttributes.Normal; File.Delete(toFile); } } } } /// /// 判断文件是否正在被使用 /// /// /// public static bool IsFileInUse(string fileName) { bool inUse = true; if (File.Exists(fileName)) { FileStream fs = null; try { fs = new FileStream(fileName, FileMode.Open, FileAccess.Write, FileShare.None); inUse = false; } catch (Exception e) { Console.WriteLine(fileName+"被其他程序占用中..."); } finally { if (fs != null) { fs.Close(); } } return inUse;//true示正在使用,false没有使用 } else { return false;//文件不存在则一定没有被使用 } } } }
/
本文档为【Console程序移动文件】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。 本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。 网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。

历史搜索

    清空历史搜索