1 文件及文件夹的操作
1.1 文件的常用操作
string strCurrentPath = AppDomain. CurrentDomain. BaseDirectory;
Console. WriteLine ( strCurrentPath) ;
{
if ( ! Directory. Exists ( strCurrentPath) )
{
Directory. CreateDirectory ( strCurrentPath) ;
Directory. Move ( strCurrentPath, "" ) ;
Directory. Delete ( strCurrentPath) ;
}
DirectoryInfo dirInfo = new DirectoryInfo ( strCurrentPath) ;
Console. WriteLine ( string . Format ( "{0}{1}{2}" , dirInfo. FullName, dirInfo. CreationTime, dirInfo. LastAccessTime) ) ;
}
{
DriveInfo[ ] driveInfos = DriveInfo. GetDrives ( ) ;
foreach ( DriveInfo item in driveInfos)
{
if ( item. IsReady)
{
Console. WriteLine ( "Type:{0},卷标:{1},Name:{2},总空间:{3},剩余空间:{4}" , item. DriveType, item. VolumeLabel, item. Name, item. TotalSize, item. AvailableFreeSpace) ;
}
else
{
Console. WriteLine ( "Type:{0} is Ready" , item. DriveType) ;
}
}
}
#region 剪切非空目录
public static void CutDirectory ( string strSourcePath, string strDestPath)
{
if ( ! Directory. Exists ( strSourcePath) || ! Directory. Exists ( strDestPath) )
{
return ;
}
DirectoryInfo sourcePathInfo = new DirectoryInfo ( strSourcePath) ;
string srcxDirName = sourcePathInfo. Name;
string path = Path. Combine ( strDestPath, srcxDirName) ;
if ( ! Directory. Exists ( path) )
{
DirectorySecurity securityRules = new DirectorySecurity ( ) ;
securityRules. AddAccessRule ( new FileSystemAccessRule ( @"Everyone" , FileSystemRights. FullControl, AccessControlType. Allow) ) ;
Directory. CreateDirectory ( path, securityRules) ;
}
List< FileSystemInfo> files = GetListFiles ( sourcePathInfo) ;
foreach ( FileSystemInfo fileInfo in files)
{
if ( File. Exists ( fileInfo. FullName) )
{
File. Move ( fileInfo. FullName, Path. Combine ( path, fileInfo. Name) ) ;
}
else if ( Directory. Exists ( fileInfo. FullName) )
{
CutDirectory ( fileInfo. FullName, path) ;
}
}
Directory. Delete ( strSourcePath, true ) ;
}
#endregion
#region 删除给定目录下非空文件夹
public static void DeleteDirectory ( string strPath)
{
if ( ! Directory. Exists ( strPath) )
{
return ;
}
DirectoryInfo directoryInfo = new DirectoryInfo ( strPath) ;
List< FileSystemInfo> files = GetListFiles ( directoryInfo) ;
foreach ( FileSystemInfo fileInfo in files)
{
if ( File. Exists ( fileInfo. FullName) )
{
File. Delete ( fileInfo. FullName) ;
}
else if ( Directory. Exists ( fileInfo. FullName) )
{
DeleteDirectory ( fileInfo. FullName) ;
}
}
Directory. Delete ( strPath) ;
}
#endregion
#region 列出文件夹的子孙文件与目录
public static void ListFiles ( DirectoryInfo directoryInfo, string strSpace)
{
List< FileSystemInfo> files = GetListFiles ( directoryInfo) ;
foreach ( FileSystemInfo fileInfo in files)
{
if ( File. Exists ( fileInfo. FullName) )
{
Console. WriteLine ( $"{strSpace}+{fileInfo.Name}" ) ;
}
else if ( Directory. Exists ( fileInfo. FullName) )
{
Console. WriteLine ( $"{strSpace}+{fileInfo.Name}" ) ;
ListFiles ( new DirectoryInfo ( fileInfo. FullName) , "| " + strSpace) ;
}
}
}
#endregion
#region 获取文件夹下文件和文件夹
private static List< FileSystemInfo> GetListFiles ( DirectoryInfo directoryInfo)
{
List< FileSystemInfo> listFile = new List < FileSystemInfo > ( ) ;
DirectoryInfo[ ] directoryInfos = directoryInfo. GetDirectories ( ) ;
foreach ( DirectoryInfo dirInfo in directoryInfos)
{
listFile. Add ( dirInfo) ;
}
FileInfo[ ] fileInfos = directoryInfo. GetFiles ( ) ;
foreach ( FileInfo fileInfo in fileInfos)
{
listFile. Add ( fileInfo) ;
}
return listFile;
}
#endregion
static void PrintFileVersionInfo ( string path)
{
System. IO. FileInfo fileInfo = null ;
try
{
fileInfo = new System. IO. FileInfo ( path) ;
}
catch ( Exception e)
{
Console. WriteLine ( e. Message) ;
}
if ( fileInfo != null && fileInfo. Exists)
{
System. Diagnostics. FileVersionInfo info = System. Diagnostics. FileVersionInfo. GetVersionInfo ( path) ;
Console. WriteLine ( "文件名称=" + info. FileName) ;
Console. WriteLine ( "产品名称=" + info. ProductName) ;
Console. WriteLine ( "公司名称=" + info. CompanyName) ;
Console. WriteLine ( "文件版本=" + info. FileVersion) ;
Console. WriteLine ( "产品版本=" + info. ProductVersion) ;
Console. WriteLine ( "系统显示文件版本:" + info. ProductMajorPart + '.' + info. ProductMinorPart + '.' + info. ProductBuildPart + '.' + info. ProductPrivatePart) ;
Console. WriteLine ( "文件说明=" + info. FileDescription) ;
Console. WriteLine ( "文件语言=" + info. Language) ;
Console. WriteLine ( "原始文件名称=" + info. OriginalFilename) ;
Console. WriteLine ( "文件版权=" + info. LegalCopyright) ;
Console. WriteLine ( "文件大小=" + System. Math. Ceiling ( fileInfo. Length / 1024.0 ) + " KB" ) ;
}
else
{
Console. WriteLine ( "指定的文件路径不正确!" ) ;
}
Console. WriteLine ( ) ;
}
1.2 遍历文件夹中所有的子文件夹
private void GetChildDirectoryInfo ( List< DirectoryInfo> directoryInfoList, DirectoryInfo directoryInfo)
{
DirectoryInfo[ ] directoryInfos = directoryInfo. GetDirectories ( ) ;
if ( directoryInfos!= null && directoryInfos. Length> 0 )
{
directoryInfoList. AddRange ( directoryInfos) ;
foreach ( var item in directoryInfos)
{
GetChildDirectoryInfo ( directoryInfoList, item) ;
}
}
}
1.3 获取计算机中的标卷
#region 计算机上的所有逻辑驱动器的驱动器名称
private void Test03 ( )
{
DriveInfo[ ] driveInfos = DriveInfo. GetDrives ( ) ;
foreach ( var item in driveInfos)
{
if ( item. IsReady)
{
Console. WriteLine ( $"type:{item.DriveType},卷标:{item.VolumeLabel},Name:{item.Name},总空间:{item.TotalSize / (1024 * 1024 * 1024)},剩余空间:{item.AvailableFreeSpace / (1024 * 1024 * 1024)}" ) ;
}
else
{
Console. WriteLine ( $"Type:{item.DriveType} not Ready" ) ;
}
}
}
#endregion
2 对象的序列化与反序列化
前期准备
class Constant
{
public static string g_strSerialDataPath = @"C:\Users\czswq\Desktop\test" ;
}
[ Serializable ]
class Programmer
{
[ NonSerialized ]
public int ID = 1 ;
public string Description { get ; set ; }
public string Name { get ; set ; }
public string Sex { get ; set ; }
}
class DataFactory
{
public static List< Programmer> BuildProgrammerList ( )
{
List< Programmer> list = new List < Programmer > ( ) ;
list. Add ( new Programmer ( ) { ID= 1 , Description= "教师" , Name= "李四" , Sex= "女" } ) ;
list. Add ( new Programmer ( ) { ID = 2 , Description = "学生" , Name = "张三" , Sex = "男" } ) ;
list. Add ( new Programmer ( ) { ID = 1 , Description = "校长" , Name = "王五" , Sex = "女" } ) ;
return list;
}
}
class JsonHelper
{
private static readonly object JavaScriptSerializer;
internal static string ObjectToString < T > ( T programmerList)
{
JavaScriptSerializer jjs = new JavaScriptSerializer ( ) ;
return jjs. Serialize ( programmerList) ;
}
internal static T StringToObject < T > ( string strResult)
{
JavaScriptSerializer jss = new System. Web. Script. Serialization. JavaScriptSerializer ( ) ;
return jss. Deserialize < T > ( strResult) ;
}
public static string ToJson < T > ( T obj)
{
return JsonConvert. SerializeObject ( obj) ;
}
public static T ToObject < T > ( string content)
{
return JsonConvert. DeserializeObject < T > ( content) ;
}
}
2.1 二进制序列化器(文件乱码,看不懂)
#region 使用二进制序列化对象
private void BinarySerialize ( )
{
string fileName = Path. Combine ( Constant. g_strSerialDataPath, @"kk.txt" ) ;
using ( Stream fStream = new FileStream ( fileName, FileMode. Create, FileAccess. ReadWrite) )
{
List< Programmer> programmerList = DataFactory. BuildProgrammerList ( ) ;
BinaryFormatter binaryFormatter = new BinaryFormatter ( ) ;
binaryFormatter. Serialize ( fStream, programmerList) ;
}
using ( Stream fStream = new FileStream ( fileName, FileMode. Open, FileAccess. Read) )
{
BinaryFormatter binaryFormatter = new BinaryFormatter ( ) ;
fStream. Position = 0 ;
List< Programmer> list = ( List< Programmer> ) binaryFormatter. Deserialize ( fStream) ;
}
}
#endregion
2.2 Soap序列化器
#region Soap序列化器
private void SoapSerialize ( )
{
string fileName = Path. Combine ( Constant. g_strSerialDataPath, @"kkSoap.txt" ) ;
using ( Stream fStream = new FileStream ( fileName, FileMode. Create, FileAccess. ReadWrite) )
{
List< Programmer> programmerList = DataFactory. BuildProgrammerList ( ) ;
SoapFormatter soapFormatter = new SoapFormatter ( ) ;
soapFormatter. Serialize ( fStream, programmerList. ToArray ( ) ) ;
}
using ( Stream fStream = new FileStream ( fileName, FileMode. Open, FileAccess. Read) )
{
SoapFormatter soapFormatter = new SoapFormatter ( ) ;
fStream. Position = 0 ;
List< Programmer> list = ( ( Programmer[ ] ) soapFormatter. Deserialize ( fStream) ) . ToList ( ) ;
}
}
#endregion
2.3 Xml序列化器
#region Xml序列化器
private void XmlSerialize ( )
{
string fileName = Path. Combine ( Constant. g_strSerialDataPath, @"kkXml.txt" ) ;
using ( Stream fStream = new FileStream ( fileName, FileMode. Create, FileAccess. ReadWrite) )
{
List< Programmer> programmerList = DataFactory. BuildProgrammerList ( ) ;
XmlSerializer xmlSerializer = new XmlSerializer ( typeof ( List< Programmer> ) ) ;
xmlSerializer. Serialize ( fStream, programmerList) ;
}
using ( Stream fStream = new FileStream ( fileName, FileMode. Open, FileAccess. Read) )
{
XmlSerializer xmlSerializer = new XmlSerializer ( typeof ( List< Programmer> ) ) ;
fStream. Position = 0 ;
List< Programmer> list = ( List< Programmer> ) xmlSerializer. Deserialize ( fStream) ;
}
}
#endregion
2.4 Json序列化对象
#region Json序列化
private void jsonSerialize ( )
{
List< Programmer> programmerList = DataFactory. BuildProgrammerList ( ) ;
string strResult = JsonHelper. ObjectToString< List< Programmer> > ( programmerList) ;
List< Programmer> list = JsonHelper. StringToObject< List< Programmer> > ( strResult) ;
}
#endregion