本文整理匯總了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);
}
}
}
}