本文整理汇总了C#中System.IO.DirectoryInfo类的典型用法代码示例。如果您正苦于以下问题:C# DirectoryInfo类的具体用法?C# DirectoryInfo怎么用?C# DirectoryInfo使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
DirectoryInfo类属于System.IO命名空间,在下文中一共展示了DirectoryInfo类的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
using System;
using System.IO;
class Test
{
public static void Main()
{
// Specify the directories you want to manipulate.
DirectoryInfo di = new DirectoryInfo(@"c:\MyDir");
try
{
// Determine whether the directory exists.
if (di.Exists)
{
// Indicate that the directory already exists.
Console.WriteLine("That path exists already.");
return;
}
// Try to create the directory.
di.Create();
Console.WriteLine("The directory was created successfully.");
// Delete the directory.
di.Delete();
Console.WriteLine("The directory was deleted successfully.");
}
catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
}
finally {}
}
}
示例2: CopyAll
//引入命名空间
using System;
using System.IO;
class CopyDir
{
public static void CopyAll(DirectoryInfo source, DirectoryInfo target)
{
if (source.FullName.ToLower() == target.FullName.ToLower())
{
return;
}
// Check if the target directory exists, if not, create it.
if (Directory.Exists(target.FullName) == false)
{
Directory.CreateDirectory(target.FullName);
}
// Copy each file into it's new directory.
foreach (FileInfo fi in source.GetFiles())
{
Console.WriteLine(@"Copying {0}\{1}", target.FullName, fi.Name);
fi.CopyTo(Path.Combine(target.ToString(), fi.Name), true);
}
// Copy each subdirectory using recursion.
foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
{
DirectoryInfo nextTargetSubDir =
target.CreateSubdirectory(diSourceSubDir.Name);
CopyAll(diSourceSubDir, nextTargetSubDir);
}
}
public static void Main()
{
string sourceDirectory = @"c:\sourceDirectory";
string targetDirectory = @"c:\targetDirectory";
DirectoryInfo diSource = new DirectoryInfo(sourceDirectory);
DirectoryInfo diTarget = new DirectoryInfo(targetDirectory);
CopyAll(diSource, diTarget);
}
// Output will vary based on the contents of the source directory.
}
示例3: new DirectoryInfo(String path)
//引入命名空间
using System;
using System.IO;
public class WildcardTest {
private static void Main(string[] args) {
DirectoryInfo dir = new DirectoryInfo("c:\\");
FileInfo[] files = dir.GetFiles("*");
foreach (FileInfo file in files) {
Console.Write("Name: " + file.Name + " ");
Console.WriteLine("Size: " + file.Length.ToString());
}
}
}