本文整理汇总了C#中System.IO.Directory类的典型用法代码示例。如果您正苦于以下问题:C# Directory类的具体用法?C# Directory怎么用?C# Directory使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Directory类属于System.IO命名空间,在下文中一共展示了Directory类的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
using System;
using System.IO;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
string sourceDirectory = @"C:\current";
string archiveDirectory = @"C:\archive";
try
{
var txtFiles = Directory.EnumerateFiles(sourceDirectory, "*.txt");
foreach (string currentFile in txtFiles)
{
string fileName = currentFile.Substring(sourceDirectory.Length + 1);
Directory.Move(currentFile, Path.Combine(archiveDirectory, fileName));
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}
示例2: Main
//引入命名空间
using System;
using System.IO;
using System.Linq;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
string archiveDirectory = @"C:\archive";
var files = from retrievedFile in Directory.EnumerateFiles(archiveDirectory, "*.txt", SearchOption.AllDirectories)
from line in File.ReadLines(retrievedFile)
where line.Contains("Example")
select new
{
File = retrievedFile,
Line = line
};
foreach (var f in files)
{
Console.WriteLine("{0} contains {1}", f.File, f.Line);
}
Console.WriteLine("{0} lines found.", files.Count().ToString());
}
}
}
示例3: Main
//引入命名空间
using System;
using System.IO;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
string sourceDirectory = @"C:\source";
string destinationDirectory = @"C:\destination";
try
{
Directory.Move(sourceDirectory, destinationDirectory);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}