asp.net 文件 操作方法

本文提供了文件及文件夹操作的方法总结,包括移动文件、批量移动文件、删除文件与文件夹、移动文件夹等实用功能的实现代码。这些方法适用于.NET平台,并通过具体的C#代码示例展示了如何进行文件管理。
 1         /// <summary>
 2         /// 移动文件
 3         /// </summary>
 4         /// <param name="oldPath">源文件路径</param>
 5         /// <param name="newPath">目标文件路径</param>
 6         /// <param name="fileName">文件名称</param>
 7         public static void MoveFile(string oldPath, string newPath, string fileName)
 8         {
 9             if (!Directory.Exists(newPath))
10             {
11                 //不存在则自动创建文件夹
12                 Directory.CreateDirectory(newPath);
13             }
14             File.Move(oldPath + fileName, newPath + fileName);
15         }
16 
17         /// <summary>
18         /// 批量移动文件
19         /// </summary>
20         /// <param name="oldPath">源文件路径</param>
21         /// <param name="newPath">目标文件路径</param>
22         /// <param name="fileNameList">文件名称</param>
23         public static void MoveFile(string oldPath, string newPath, ArrayList fileNameList)
24         {
25             if (!Directory.Exists(newPath))
26             {
27                 //不存在则自动创建文件夹
28                 Directory.CreateDirectory(newPath);
29             }
30             for (int i = 0; i < fileNameList.Count; i++)
31             {
32                 File.Move(oldPath + fileNameList[i], newPath + fileNameList[i]);
33             }
34         }
35 
36         /// <summary>
37         /// 删除文件
38         /// </summary>
39         /// <param name="path">文件路径</param>
40         /// <returns>删除结果,成功或失败</returns>
41         public static bool DeleteFile(string path)
42         {
43             try
44             {
45                 File.Delete(path);
46                 return true;
47             }
48             catch
49             {
50                 return false;
51             }
52         }
53 
54         /// <summary>
55         /// 删除文件夹
56         /// </summary>
57         /// <param name="path">文件夹路径</param>
58         /// <returns>删除结果,成功或失败</returns>
59         public static bool DeleteFolder(string path)
60         {
61             try
62             {
63                 Directory.Delete(path);
64                 return true;
65             }
66             catch
67             {
68                 return false;
69             }
70         }
71 
72         /// <summary>
73         /// 移动文件夹
74         /// </summary>
75         /// <param name="oldPath">源文件夹路径</param>
76         /// <param name="newPath">目标文件夹路径</param>
77         /// <returns>移动结果</returns>
78         public static bool MoveFolder(string oldPath, string newPath)
79         {
80             try
81             {
82                 Directory.Move(oldPath, newPath);
83                 return true;
84             }
85             catch
86             {
87                 return false;
88             }
89         }    

 

转载于:https://www.cnblogs.com/linJie1930906722/p/5525538.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值