對目錄進行操作
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace Humep.FileWatcher
{
///
///
///
public static class DirectoryHelper
{
///
/// 初始化目錄
///
///
///
public static bool CreateDirectory(string directory)
{
if (string.IsNullOrWhiteSpace(directory))
{
return false;
}
{
///
///
///
public static class DirectoryHelper
{
///
/// 初始化目錄
///
///
///
public static bool CreateDirectory(string directory)
{
if (string.IsNullOrWhiteSpace(directory))
{
return false;
}
if (Directory.Exists(directory))
{
return true;
}
{
return true;
}
var dirInfo = Directory.CreateDirectory(directory);
return dirInfo != null;
}
return dirInfo != null;
}
///
/// 根據當前日組合資料夾名稱
///
///
///
public static string BuildDateDirectoryName(DateTime dateTime)
{
return dateTime.ToString("yyyyMMdd");
}
/// 根據當前日組合資料夾名稱
///
///
///
public static string BuildDateDirectoryName(DateTime dateTime)
{
return dateTime.ToString("yyyyMMdd");
}
///
///
///
///
public static string BuildTodayDirectoryName()
{
return BuildDateDirectoryName(DateTime.Now);
}
///
///
///
public static string BuildTodayDirectoryName()
{
return BuildDateDirectoryName(DateTime.Now);
}
///
/// 獲取目錄名稱
///
///
///
public static string GetDirectoryName(string directory)
{
if (!Directory.Exists(directory))
{
return string.Empty ;// DirectoryHelper.CreateDirectory(directory);
}
/// 獲取目錄名稱
///
///
///
public static string GetDirectoryName(string directory)
{
if (!Directory.Exists(directory))
{
return string.Empty ;// DirectoryHelper.CreateDirectory(directory);
}
return new DirectoryInfo(directory).Name;
}
}
///
/// 獲取目錄資料夾下的所有子目錄
///
///
///
///
public static List FindSubDirectories(string directory, int maxCount)
{
List subDirectories = new List();
/// 獲取目錄資料夾下的所有子目錄
///
///
///
///
public static List
{
List
if (string.IsNullOrEmpty(directory))
{
return subDirectories;
}
{
return subDirectories;
}
if (maxCount <= 0)
{
return subDirectories;
}
{
return subDirectories;
}
string[] directories = Directory.GetDirectories(directory);
foreach (string subDirectory in directories)
{
if (subDirectories.Count == maxCount)
{
break;
}
{
if (subDirectories.Count == maxCount)
{
break;
}
subDirectories.Add(subDirectory);
}
}
return subDirectories;
}
}
public static List FindSubDirectories(string directory)
{
return Directory.GetDirectories(directory, "*", SearchOption.AllDirectories).ToList();
}
{
return Directory.GetDirectories(directory, "*", SearchOption.AllDirectories).ToList
}
///
/// 根據時間查詢子目錄
///
///
///
///
public static List FindSubDirectories(string directory, int maxCount, int days)
{
List subDirectories = new List();
/// 根據時間查詢子目錄
///
///
///
///
public static List
{
List
if (string.IsNullOrEmpty(directory))
{
return subDirectories;
}
{
return subDirectories;
}
if (maxCount <= 0)
{
return subDirectories;
}
{
return subDirectories;
}
string[] directories = Directory.GetDirectories(directory);
DateTime lastTime = DateTime.Now.AddDays(-Math.Abs(days));
DateTime lastTime = DateTime.Now.AddDays(-Math.Abs(days));
foreach (string subDirectory in directories)
{
if (subDirectories.Count == maxCount)
{
break;
}
DirectoryInfo dirInfo = new DirectoryInfo(subDirectory);
{
if (subDirectories.Count == maxCount)
{
break;
}
DirectoryInfo dirInfo = new DirectoryInfo(subDirectory);
if (dirInfo.LastWriteTime >= lastTime)
{
subDirectories.Add(subDirectory);
}
}
{
subDirectories.Add(subDirectory);
}
}
return subDirectories;
}
}
///
/// 將原目錄移動到目標目錄
///
///
///
///
public static bool MoveDirectory(string sourceDirectory, string targetDirectory)
{
if (string.IsNullOrEmpty(sourceDirectory) || string.IsNullOrEmpty(targetDirectory))
{
return false;
}
/// 將原目錄移動到目標目錄
///
///
///
///
public static bool MoveDirectory(string sourceDirectory, string targetDirectory)
{
if (string.IsNullOrEmpty(sourceDirectory) || string.IsNullOrEmpty(targetDirectory))
{
return false;
}
string laseMoveDirectory = string.Format("{0}\\{1}", targetDirectory, DirectoryHelper.GetDirectoryName(sourceDirectory));
while (Directory.Exists(laseMoveDirectory))
{
laseMoveDirectory = DirectoryHelper.Rename(laseMoveDirectory);
}
{
laseMoveDirectory = DirectoryHelper.Rename(laseMoveDirectory);
}
Directory.Move(sourceDirectory, laseMoveDirectory);
return true;
}
return true;
}
///
/// 重新生成新的檔案路徑
///
///
///
public static string Rename(string filePath)
{
if (string.IsNullOrWhiteSpace(filePath))
{
return string.Empty;
}
/// 重新生成新的檔案路徑
///
///
///
public static string Rename(string filePath)
{
if (string.IsNullOrWhiteSpace(filePath))
{
return string.Empty;
}
string lastDirName = DirectoryHelper.GetDirectoryName(filePath);
//重新命名,則隨機在原來檔名後面加幾個隨機數字進行組裝成新的名字
Random random = new Random(System.DateTime.Now.Millisecond);
string randomData = random.Next().ToString();
//把原檔名的名字加上隨機數,組裝成新的檔名(避免重名)
string newlastDirName = lastDirName + randomData;
string newDirPath = string.Empty;
newDirPath = filePath.Substring(0, filePath.LastIndexOf("\\")) + "\\" + newlastDirName;
//重新命名,則隨機在原來檔名後面加幾個隨機數字進行組裝成新的名字
Random random = new Random(System.DateTime.Now.Millisecond);
string randomData = random.Next().ToString();
//把原檔名的名字加上隨機數,組裝成新的檔名(避免重名)
string newlastDirName = lastDirName + randomData;
string newDirPath = string.Empty;
newDirPath = filePath.Substring(0, filePath.LastIndexOf("\\")) + "\\" + newlastDirName;
//返回新的路徑
return newDirPath;
}
return newDirPath;
}
///
/// 獲取指定目錄下的所有檔案和資料夾大小
///
/// 目錄路徑
///string,返回所有資料夾名字
public static long GetDirectorySize(string path)
{
long dirSize = 0;
DirectoryInfo dir = new DirectoryInfo(path);
foreach (FileInfo file in dir.GetFiles())
dirSize += file.Length;
foreach (DirectoryInfo subdir in dir.GetDirectories())
dirSize += GetDirectorySize(subdir.FullName);
return dirSize;
}
}
}
/// 獲取指定目錄下的所有檔案和資料夾大小
///
/// 目錄路徑
///
public static long GetDirectorySize(string path)
{
long dirSize = 0;
DirectoryInfo dir = new DirectoryInfo(path);
foreach (FileInfo file in dir.GetFiles())
dirSize += file.Length;
foreach (DirectoryInfo subdir in dir.GetDirectories())
dirSize += GetDirectorySize(subdir.FullName);
return dirSize;
}
}
}
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28699126/viewspace-773370/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 目錄操作
- ORA-22285: 對不存在的目錄或檔案進行 FILEOPEN 操作
- 【Oracle】對RID為偶數的記錄進行操作Oracle
- Linux下如何對目錄中的檔案進行統計Linux
- [進行中] Elasticsearch 終極教程——目錄Elasticsearch
- Python中OS對目錄的操作以及引用Python
- golang對遍歷目錄操作的最佳化Golang
- 利用kvo對集合進行操作
- Linux下對檔案進行加密備份的操作記錄Linux加密
- 如何對資料目標進行分析
- ABP教程-對Person資訊進行操作
- 對table的操作進行監控
- Redis -對大資料量的 key 進行批量操作的一個記錄Redis大資料
- 16.讓對話方塊支援拖拽操作/目錄框開啟操作
- 使用T-SQL進行活動目錄查詢SQL
- 【雜談】如何對Redis進行原子操作Redis
- python可以對excel進行那些操作PythonExcel
- Python 檔案、目錄操作Python
- 在 Linux 上操作目錄Linux
- Java ArrayList操作英文書目錄Java
- Java實現檔案目錄操作書目錄Java
- 理解Path對路徑進行操作的APIAPI
- 使用Java客戶端對Redis進行操作Java客戶端Redis
- 對avalon的類名操作進行升級
- 12.目錄操作(perl筆記)筆記
- Linux之旅第一篇-目錄結構及操作目錄Linux
- JQuery對ASP.NET MVC資料進行操作jQueryASP.NETMVC
- 機器學習二——利用numpy庫對矩陣進行操作機器學習矩陣
- 使用正則不允許對整表進行操作
- 對Maxvalue上限範圍分割槽進行spilt操作
- C++中對檔案進行讀寫操作C++
- 對2個hex(16進位制)字串進行異或操作字串
- shell指令碼:建立函式並指定目錄進行下載指令碼函式
- Java IO的基礎操作書目錄Java
- blender python api 使用指令碼批次對obj物體進行渲染(obj所在目錄要有與之對應的mtl檔案才行)PythonAPI指令碼OBJ
- jquery獲得option的值和對option進行操作jQuery
- Python如何對浮點數進行取整操作?Python
- 【TcaplusDB知識庫】如何對陣列進行操作陣列