directory 提供静态方法。
directoryinfo 实例提供实例方法;
![ContractedBlock.gif](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
string
srcFileName
=
@"
D:\a.txt
"
;
string destFileName = @" D:\b.txt " ;
string srcFolderPath = @" D:\1 " ;
string destFolderPath = @" D:\6 " ;
// 方法一
if (System.IO.File.Exists(srcFileName))
{
System.IO.File.Move(srcFileName, destFileName);
}
if (System.IO.Directory.Exists(srcFolderPath))
{
System.IO.Directory.Move(srcFolderPath, destFolderPath);
}
// 方法二
if (System.IO.File.Exists(srcFileName))
{
System.IO.FileInfo file = new System.IO.FileInfo(srcFileName);
file.MoveTo(destFileName);
}
if (System.IO.Directory.Exists(srcFolderPath))
{
System.IO.DirectoryInfo folder = new System.IO.DirectoryInfo(srcFolderPath);
folder.MoveTo(destFolderPath);
}
string destFileName = @" D:\b.txt " ;
string srcFolderPath = @" D:\1 " ;
string destFolderPath = @" D:\6 " ;
// 方法一
if (System.IO.File.Exists(srcFileName))
{
System.IO.File.Move(srcFileName, destFileName);
}
if (System.IO.Directory.Exists(srcFolderPath))
{
System.IO.Directory.Move(srcFolderPath, destFolderPath);
}
// 方法二
if (System.IO.File.Exists(srcFileName))
{
System.IO.FileInfo file = new System.IO.FileInfo(srcFileName);
file.MoveTo(destFileName);
}
if (System.IO.Directory.Exists(srcFolderPath))
{
System.IO.DirectoryInfo folder = new System.IO.DirectoryInfo(srcFolderPath);
folder.MoveTo(destFolderPath);
}
方法三:
FSO实现,不用移动内部文件。
![ContractedBlock.gif](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
//
C#动态修改文件夹名称(FSO实现,不移动文件)
string lstrFileFolder = HttpContext.Current.Server.MapPath(Com.Common.m_folderData);
string subFolder = System.DateTime.Today.AddDays( - 1 ).ToString( " yyMMdd " );
string newFolder = System.DateTime.Today.ToString( " yyMMdd " );
string path = lstrFileFolder + subFolder;
// 判断目录是否存在,存在则改名
if (Directory.Exists(path))
{
Scripting.FileSystemObject fso = new Scripting.FileSystemObjectClass();
fso.GetFolder(path).Name = newFolder;
}System.IO.Directory.Move( " 源目录 " , " 新目录 " );
OR
项目- > 引用 浏览 COM组件 Microsoft Scripting Runtime ,
Scripting.FileSystemObject fso = new Scripting.FileSystemObjectClass();
fso.GetFolder( " 目录 " ).Name = " 新目录名 " ;
string lstrFileFolder = HttpContext.Current.Server.MapPath(Com.Common.m_folderData);
string subFolder = System.DateTime.Today.AddDays( - 1 ).ToString( " yyMMdd " );
string newFolder = System.DateTime.Today.ToString( " yyMMdd " );
string path = lstrFileFolder + subFolder;
// 判断目录是否存在,存在则改名
if (Directory.Exists(path))
{
Scripting.FileSystemObject fso = new Scripting.FileSystemObjectClass();
fso.GetFolder(path).Name = newFolder;
}System.IO.Directory.Move( " 源目录 " , " 新目录 " );
OR
项目- > 引用 浏览 COM组件 Microsoft Scripting Runtime ,
Scripting.FileSystemObject fso = new Scripting.FileSystemObjectClass();
fso.GetFolder( " 目录 " ).Name = " 新目录名 " ;