本文整理汇总了C#中DirectoryInfo.CreateSubdirectory方法的典型用法代码示例。如果您正苦于以下问题:C# DirectoryInfo.CreateSubdirectory方法的具体用法?C# DirectoryInfo.CreateSubdirectory怎么用?C# DirectoryInfo.CreateSubdirectory使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DirectoryInfo
的用法示例。
在下文中一共展示了DirectoryInfo.CreateSubdirectory方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CopyAll
/// <summary>
/// Recursive Copy Directory Method
/// </summary>
private static void CopyAll(DirectoryInfo source, DirectoryInfo target)
{
// Check if the source directory exists, if not, don't do any work.
if (!Directory.Exists(source.FullName))
{
return;
}
// Check if the target directory exists, if not, create it.
if (!Directory.Exists(target.FullName))
{
Directory.CreateDirectory(target.FullName);
}
// Copy each file into it’s new directory.
foreach (var fileInfo in source.GetFiles())
{
fileInfo.CopyTo (Path.Combine (target.ToString (), fileInfo.Name), true);
}
// Copy each subdirectory using recursion.
foreach (var subDirInfo in source.GetDirectories())
{
DirectoryInfo nextTargetSubDir = target.CreateSubdirectory(subDirInfo.Name);
CopyAll(subDirInfo, nextTargetSubDir);
}
}
示例2: GetEntriesThenDelete
public void GetEntriesThenDelete()
{
string testDirPath = GetTestFilePath();
DirectoryInfo testDirInfo = new DirectoryInfo(testDirPath);
testDirInfo.Create();
string testDir1 = GetTestFileName();
string testDir2 = GetTestFileName();
string testFile1 = GetTestFileName();
string testFile2 = GetTestFileName();
string testFile3 = GetTestFileName();
string testFile4 = GetTestFileName();
string testFile5 = GetTestFileName();
testDirInfo.CreateSubdirectory(testDir1);
testDirInfo.CreateSubdirectory(testDir2);
using (File.Create(Path.Combine(testDirPath, testFile1)))
using (File.Create(Path.Combine(testDirPath, testFile2)))
using (File.Create(Path.Combine(testDirPath, testFile3)))
{
string[] results;
using (File.Create(Path.Combine(testDirPath, testFile4)))
using (File.Create(Path.Combine(testDirPath, testFile5)))
{
results = GetEntries(testDirPath);
Assert.NotNull(results);
Assert.NotEmpty(results);
if (TestFiles)
{
Assert.Contains(Path.Combine(testDirPath, testFile1), results);
Assert.Contains(Path.Combine(testDirPath, testFile2), results);
Assert.Contains(Path.Combine(testDirPath, testFile3), results);
Assert.Contains(Path.Combine(testDirPath, testFile4), results);
Assert.Contains(Path.Combine(testDirPath, testFile5), results);
}
if (TestDirectories)
{
Assert.Contains(Path.Combine(testDirPath, testDir1), results);
Assert.Contains(Path.Combine(testDirPath, testDir2), results);
}
}
File.Delete(Path.Combine(testDirPath, testFile4));
File.Delete(Path.Combine(testDirPath, testFile5));
FailSafeDirectoryOperations.DeleteDirectory(testDir1, true);
results = GetEntries(testDirPath);
Assert.NotNull(results);
Assert.NotEmpty(results);
if (TestFiles)
{
Assert.Contains(Path.Combine(testDirPath, testFile1), results);
Assert.Contains(Path.Combine(testDirPath, testFile2), results);
Assert.Contains(Path.Combine(testDirPath, testFile3), results);
}
if (TestDirectories)
{
Assert.Contains(Path.Combine(testDirPath, testDir2), results);
}
}
}
示例3: GetFilesThenDelete
public void GetFilesThenDelete()
{
String testDirPath = Path.Combine(TestDirectory, GetTestFilePath());
DirectoryInfo testDirInfo = new DirectoryInfo(testDirPath);
testDirInfo.Create();
String testDir1 = GetTestFileName();
String testDir2 = GetTestFileName();
String testDir3 = GetTestFileName();
String testDir4 = GetTestFileName();
String testDir5 = GetTestFileName();
String testFile1 = GetTestFileName();
String testFile2 = GetTestFileName();
String testFile3 = GetTestFileName();
String testFile4 = GetTestFileName();
String testFile5 = GetTestFileName();
testDirInfo.CreateSubdirectory(testDir1);
testDirInfo.CreateSubdirectory(testDir2);
testDirInfo.CreateSubdirectory(testDir3);
testDirInfo.CreateSubdirectory(testDir4);
testDirInfo.CreateSubdirectory(testDir5);
using (File.Create(Path.Combine(testDirPath, testFile1)))
using (File.Create(Path.Combine(testDirPath, testFile2)))
using (File.Create(Path.Combine(testDirPath, testFile3)))
{
String[] results;
using (File.Create(Path.Combine(testDirPath, testFile4)))
using (File.Create(Path.Combine(testDirPath, testFile5)))
{
results = GetFiles(testDirPath);
Assert.NotNull(results);
Assert.NotEmpty(results);
Assert.Equal(5, results.Length);
Assert.Contains(Path.Combine(testDirPath, testFile1), results);
Assert.Contains(Path.Combine(testDirPath, testFile2), results);
Assert.Contains(Path.Combine(testDirPath, testFile3), results);
Assert.Contains(Path.Combine(testDirPath, testFile4), results);
Assert.Contains(Path.Combine(testDirPath, testFile5), results);
}
File.Delete(Path.Combine(testDirPath, testFile4));
File.Delete(Path.Combine(testDirPath, testFile5));
results = GetFiles(testDirPath);
Assert.NotNull(results);
Assert.NotEmpty(results);
Assert.Equal(3, results.Length);
Assert.Contains(Path.Combine(testDirPath, testFile1), results);
Assert.Contains(Path.Combine(testDirPath, testFile2), results);
Assert.Contains(Path.Combine(testDirPath, testFile3), results);
}
}
示例4: CopyFilesRecursively
public static void CopyFilesRecursively(DirectoryInfo source, DirectoryInfo target)
{
foreach (DirectoryInfo dir in source.GetDirectories())
CopyFilesRecursively(dir, target.CreateSubdirectory(dir.Name));
foreach (FileInfo file in source.GetFiles())
file.CopyTo(Path.Combine(target.FullName, file.Name));
}
示例5: Copy
public static void Copy(this DirectoryInfo dir, DirectoryInfo destinationDir, bool overwrite = true, bool copySubDirs = true)
{
if (!dir.Exists)
{
throw new DirectoryNotFoundException(
"Source directory does not exist or could not be found."
);
}
// If the destination directory doesn't exist, create it.
if (!destinationDir.Exists)
destinationDir.Create();
// Get the files in the directory and copy them to the new location.
foreach (FileInfo file in dir.EnumerateFiles())
{
var targetpath = destinationDir.GetFile(file.Name).FullName;
if (overwrite || !File.Exists(targetpath))
file.CopyTo(targetpath, true);
}
// If copying subdirectories, copy them and their contents to new location.
if (copySubDirs)
foreach (var subdir in dir.EnumerateDirectories())
subdir.Copy(destinationDir.CreateSubdirectory(subdir.Name));
}
示例6: CopyAll
/// <summary>
/// Verzeichnis rekursiv kopieren. Vorhandene Dateien werden überschreiben
/// </summary>
/// <param name="source">QuellVERZEICHNIS</param>
/// <param name="target">ZielVERZEICHNIS</param>
/// <param name="dirCount">für jedes erzeugte Verzeichnis +=1</param>
/// <param name="fileCount">für jede erzeugte Datei +=1</param>
public static void CopyAll(
DirectoryInfo source,
DirectoryInfo target,
ref int dirCount,
ref int fileCount)
{
// Check if the target directory exists, if not, create it.
if (!Directory.Exists(target.FullName))
{
dirCount++;
Directory.CreateDirectory(target.FullName);
Logger.Log("Verzeichnis erstellt: " + target.FullName);
}
// Copy each file into its new directory.
foreach (FileInfo fi in source.GetFiles())
{
fileCount++;
fi.CopyTo(Path.Combine(target.ToString(), fi.Name), true);
Logger.Log("Datei kopiert: " + fi.FullName);
}
// Copy each subdirectory using recursion.
foreach (DirectoryInfo diSrcSubDir in source.GetDirectories())
{
dirCount++;
DirectoryInfo diNext = target.CreateSubdirectory(diSrcSubDir.Name);
CopyAll(diSrcSubDir, diNext, ref dirCount, ref fileCount);
}
}
示例7: UploadButton_Click
protected void UploadButton_Click(object sender, EventArgs e)
{
User user = MIPS246UserManager.QueryUser(Session["LoginId"].ToString());
HomeworkRecord homeworkRecord = MIPS246HomerworkManager.QueryUserHomeworkRecord(user.StudentID);
DirectoryInfo di = new DirectoryInfo(homework);
DirectoryInfo targetdi =null;
bool hasUserFolder = false;
foreach (DirectoryInfo userdi in di.GetDirectories())
{
if (userdi.Name == user.StudentID)
{
hasUserFolder = true;
targetdi=userdi;
break;
}
}
if (hasUserFolder == false)
{
targetdi=di.CreateSubdirectory(user.StudentID);
}
targetdi=targetdi.CreateSubdirectory(Request["index"].ToString());
if (FileUploader.HasFile)
{
FileUploader.PostedFile.SaveAs(targetdi.FullName + "\\" + FileUploader.FileName);
MIPS246HomerworkManager.UpdateHomeworkRecord(user.StudentID, int.Parse(Request["index"].ToString()));
Response.Redirect("submitHomework.aspx");
}
}
示例8: SearchPatternSubset
public void SearchPatternSubset()
{
string testDir1Str = GetTestFileName();
string testDir11Str = GetTestFileName();
DirectoryInfo testDir = new DirectoryInfo(TestDirectory);
DirectoryInfo testDir1 = testDir.CreateSubdirectory(testDir1Str);
testDir1.CreateSubdirectory(testDir11Str);
string testFile1 = GetTestFileName();
string testFile2 = "ca1" + testFile1;
testFile1 = "ca2" + testFile1;
string testFile3 = GetTestFileName();
string searchPattern = string.Format("???{0}", testFile1.Substring(3));
using (File.Create(Path.Combine(TestDirectory, testDir1Str, testDir11Str, GetTestFileName())))
using (File.Create(Path.Combine(TestDirectory, testDir1Str, testFile1)))
using (File.Create(Path.Combine(TestDirectory, testDir1Str, testFile2)))
using (File.Create(Path.Combine(TestDirectory, testDir1Str, testFile3)))
{
string[] results = GetEntries(Path.Combine(TestDirectory, testDir1Str), searchPattern);
Assert.Equal(2, results.Length);
Assert.Contains(Path.Combine(TestDirectory, testDir1Str, testFile1), results);
Assert.Contains(Path.Combine(TestDirectory, testDir1Str, testFile2), results);
//suffix only should return the empty array
Assert.Empty(GetEntries(Path.Combine(TestDirectory, testDir1Str), testFile1.Substring(3)));
}
}
示例9: Main
public static void Main(string[] args)
{
FileInfo file = new FileInfo(@"..\..\CopyDelete.cs");
string fullPath =file.FullName;
DirectoryInfo dir = new DirectoryInfo(@"..\..\");
dir.CreateSubdirectory("backup");
DirectoryInfo backupDir = new DirectoryInfo(@"..\..\backup");
foreach (FileInfo subfile in backupDir.GetFiles())
{
subfile.Delete();
}
file.CopyTo(@"..\..\backup\CopyDelete_Backup.cs");
DirectoryInfo tempDir = new DirectoryInfo(@"..\..\temp");
tempDir.Create();
FileInfo tempFile = new FileInfo(@"..\..\temp\temp.txt");
FileStream fs = tempFile.Create();
fs.Close();
tempFile.Delete();
tempDir.Delete(true);
Console.WriteLine("All done.");
Console.ReadLine();
}
示例10: SearchPatternWithTrailingStar
public void SearchPatternWithTrailingStar()
{
DirectoryInfo testDir = new DirectoryInfo(TestDirectory);
testDir.CreateSubdirectory("TestDir1");
testDir.CreateSubdirectory("TestDir2");
testDir.CreateSubdirectory("TestDir3");
using (File.Create(Path.Combine(TestDirectory, "TestFile1")))
using (File.Create(Path.Combine(TestDirectory, "TestFile2")))
using (File.Create(Path.Combine(TestDirectory, "Test1File2")))
using (File.Create(Path.Combine(TestDirectory, "Test1Dir2")))
{
String[] strArr = GetEntries(TestDirectory, "Test1*");
Assert.Equal(2, strArr.Length);
Assert.Contains(Path.Combine(TestDirectory, "Test1File2"), strArr);
Assert.Contains(Path.Combine(TestDirectory, "Test1Dir2"), strArr);
}
}
示例11: CopyFilesRecursively
public static void CopyFilesRecursively(DirectoryInfo source, DirectoryInfo target)
{
// From http://stackoverflow.com/questions/58744/best-way-to-copy-the-entire-contents-of-a-directory-in-c/58779#58779
foreach (var dir in source.GetDirectories())
{
CopyFilesRecursively(dir, target.CreateSubdirectory(Rename(dir.Name)));
}
foreach (var file in source.GetFiles())
{
file.CopyTo(Path.Combine(target.FullName, Rename(file.Name)));
}
}
示例12: WindowsSearchPatternQuestionMarks
public override void WindowsSearchPatternQuestionMarks()
{
string testDir1Str = GetTestFileName();
DirectoryInfo testDir = new DirectoryInfo(TestDirectory);
DirectoryInfo testDir1 = testDir.CreateSubdirectory(testDir1Str);
using (File.Create(Path.Combine(TestDirectory, testDir1Str, GetTestFileName())))
using (File.Create(Path.Combine(TestDirectory, GetTestFileName())))
{
string[] results = GetEntries(TestDirectory, string.Format("{0}.???", new string('?', GetTestFileName().Length)));
if (TestFiles && TestDirectories)
Assert.Equal(3, results.Length);
else
Assert.Equal(1, results.Length);
}
}
示例13: CopyAll
private static void CopyAll( DirectoryInfo source, DirectoryInfo target)
{
// Check if the target directory exists; if not, create it.
if (Directory.Exists(target.FullName) == false)
{
Directory.CreateDirectory(target.FullName);
}
// Copy each file into the new directory.
foreach (FileInfo fi in source.GetFiles())
{
Console.WriteLine(@"Copying {0}\{1}", target.FullName, fi.Name);
fi.CopyTo(Path.Combine(target.FullName, fi.Name), true);
}
// Copy each subdirectory using recursion.
foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
{
DirectoryInfo nextTargetSubDir =
target.CreateSubdirectory(diSourceSubDir.Name);
CopyAll(diSourceSubDir, nextTargetSubDir);
}
}
示例14: CopyAll
/// <summary>
/// Recursive Copy Directory Method
/// </summary>
public static void CopyAll(DirectoryInfo source, DirectoryInfo target)
{
// Check if the target directory exists, if not, create it.
if (Directory.Exists(target.FullName) == false)
{
dircount++;
Directory.CreateDirectory(target.FullName);
}
// Copy each file into it’s new directory.
foreach (FileInfo fi in source.GetFiles())
{
filecount++;
fi.CopyTo(Path.Combine(target.ToString(), fi.Name), true);
}
// Copy each subdirectory using recursion.
foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
{
dircount++;
DirectoryInfo nextTargetSubDir = target.CreateSubdirectory(diSourceSubDir.Name);
CopyAll(diSourceSubDir, nextTargetSubDir);
}
}
示例15: runTest
public bool runTest()
{
Console.WriteLine(s_strTFPath + "\\" + s_strTFName + " , for " + s_strClassMethod + " , Source ver " + s_strDtTmVer);
int iCountErrors = 0;
int iCountTestcases = 0;
String strLoc = "Loc_000oo";
String strValue = String.Empty;
try
{
DirectoryInfo dir2;
String dirName = s_strTFAbbrev+"TestDir";
String[] strArr;
if(Directory.Exists(dirName))
Directory.Delete(dirName, true);
strLoc = "Loc_477g8";
dir2 = new DirectoryInfo(".");
iCountTestcases++;
try {
Directory.GetFiles(null, "*");
iCountErrors++;
printerr( "Error_2988b! Expected exception not thrown");
} catch (ArgumentNullException aexc) {
printinfo( "Info_29087! Caught expected exception, aexc=="+aexc.Message);
} catch (Exception exc) {
iCountErrors++;
printerr( "Error_0707t! Incorrect exception thrown, exc=="+exc.ToString());
}
iCountTestcases++;
try {
Directory.GetFiles(".", null);
iCountErrors++;
printerr( "Error_2y867! Expected exception not thrown");
} catch (ArgumentNullException aexc) {
printinfo( "Info_2875b! Caught expected exception, aexc=="+aexc.Message);
} catch (Exception exc) {
iCountErrors++;
printerr( "Error_39yb7! Incorrect exception thrown, exc=="+exc.ToString());
}
strLoc = "Loc_4yg7b";
dir2 = new DirectoryInfo(".");
iCountTestcases++;
try {
Directory.GetFiles(String.Empty, "*");
iCountErrors++;
printerr( "Error_8ytbm! Expected exception not thrown");
} catch (ArgumentException aexc) {
printinfo( "Info_y687d! Caught expected, exc=="+aexc.Message);
} catch (Exception exc) {
iCountErrors++;
printerr( "Error_2908y! Incorrect exception thrown, exc=="+exc.ToString());
}
iCountTestcases++;
try {
String[] strFiles = Directory.GetFiles(".", String.Empty);
if ( strFiles.Length != 0 ) {
iCountErrors++;
printerr( "Error_478b8! Incorrect number of files");
}
} catch (Exception exc) {
iCountErrors++;
printerr( "Error_21999! Incorrect exception thrown, exc=="+exc.ToString());
}
strLoc = "Loc_1190x";
iCountTestcases++;
try {
String[] str = Directory.GetFiles(".", " ");
Console.WriteLine("Number of files :: " + str.Length );
if ( str.Length != 0 ) {
iCountErrors++;
printerr( "Error_43432! Incorrect number of files");
}
} catch (Exception exc) {
iCountErrors++;
printerr( "Error_17888! Incorrect exception thrown, exc=="+exc.ToString());
}
iCountTestcases++;
try {
Directory.GetFiles(" " , ".");
iCountErrors++;
printerr( "Error_29019! Expected exception not thrown" );
} catch(ArgumentException iexc) {
printinfo( "Info_6867b! Caught expected exception, iexc=="+iexc.Message);
} catch (Exception exc) {
iCountErrors++;
printerr( "Error_9678g! Incorrect exception thrown, exc=="+exc.ToString());
}
strLoc = "Loc_4y982";
dir2 = Directory.CreateDirectory(dirName);
strArr = Directory.GetFiles(Environment.CurrentDirectory+"\\"+dirName, "*");
iCountTestcases++;
if(strArr.Length != 0) {
iCountErrors++;
printerr("Error_207v7! Incorrect number of files returned");
}
strLoc = "Loc_2398c";
dir2.CreateSubdirectory("TestDir1");
dir2.CreateSubdirectory("TestDir2");
dir2.CreateSubdirectory("TestDir3");
dir2.CreateSubdirectory("Test1Dir1");
dir2.CreateSubdirectory("Test1Dir2");
//.........这里部分代码省略.........