對目錄進行操作
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/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 目錄操作
- Python中OS對目錄的操作以及引用Python
- Linux下如何對目錄中的檔案進行統計Linux
- golang對遍歷目錄操作的最佳化Golang
- [進行中] Elasticsearch 終極教程——目錄Elasticsearch
- 如何對資料目標進行分析
- 16.讓對話方塊支援拖拽操作/目錄框開啟操作
- python可以對excel進行那些操作PythonExcel
- 在 Linux 上操作目錄Linux
- Python 檔案、目錄操作Python
- Redis -對大資料量的 key 進行批量操作的一個記錄Redis大資料
- 【雜談】如何對Redis進行原子操作Redis
- 使用Powershell對目標進行螢幕監控
- Linux之旅第一篇-目錄結構及操作目錄Linux
- 理解Path對路徑進行操作的APIAPI
- Python如何對浮點數進行取整操作?Python
- 【TcaplusDB知識庫】如何對陣列進行操作陣列
- JQuery對ASP.NET MVC資料進行操作jQueryASP.NETMVC
- Linux中操作工作目錄和檔案目錄Linux
- Linux 目錄結構及詳細操作Linux
- 【linux0.12】目錄項管理與操作Linux
- blender python api 使用指令碼批次對obj物體進行渲染(obj所在目錄要有與之對應的mtl檔案才行)PythonAPI指令碼OBJ
- [20190403]linux快速進入目錄.txtLinux
- Git對新增目錄的處理Git
- jquery獲得option的值和對option進行操作jQuery
- python基本操作-檔案、目錄及路徑Python
- python 根據時間戳建立目錄操作Python時間戳
- 無緩衝檔案IO和目錄操作
- 【實操案例】Linux中切換目錄如何操作?Linux
- linux快捷進入長目錄的方法Linux
- [20210107]快速進入目錄2.txt
- 如何對報表資料新增目錄
- 第二章 檔案和目錄操作命令
- Python shutil用法 複製檔案與目錄操作Python
- linux伺服器目錄檔案的命令操作Linux伺服器
- 機器學習二——利用numpy庫對矩陣進行操作機器學習矩陣
- 使用nodejs對Marketing Cloud的contact主資料進行修改操作NodeJSCloud
- 使用jaxp解析器dom方式對xml節點進行操作XML
- Linux宿主目錄指什麼?如何進入?Linux