本文整理汇总了C#中DirectoryInfo.Create方法的典型用法代码示例。如果您正苦于以下问题:C# DirectoryInfo.Create方法的具体用法?C# DirectoryInfo.Create怎么用?C# DirectoryInfo.Create使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DirectoryInfo
的用法示例。
在下文中一共展示了DirectoryInfo.Create方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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));
}
示例2: Page_Load
protected void Page_Load(object sender, EventArgs e)
{
CatagoryTableAdapter adp = new CatagoryTableAdapter();
DataTable dt = adp.GetCatagoryName();
DirectoryInfo diAsset = new DirectoryInfo(Server.MapPath("Assets"));
for (int i = 0; i < dt.Rows.Count; i++)
{
string catagoryFolderName = dt.Rows[i][0].ToString();
DirectoryInfo catagoryFolderDir = new DirectoryInfo(diAsset.FullName + "/" + catagoryFolderName);
if (!catagoryFolderDir.Exists)
{
catagoryFolderDir.Create();
}
SubcatagoryTableAdapter subTa = new SubcatagoryTableAdapter();
DataTable subDt = subTa.GetSubCatagoryNames(catagoryFolderName);
for (int j = 0; j < subDt.Rows.Count; j++)
{
string subCatagoryFolderName = subDt.Rows[j][2].ToString();
DirectoryInfo subCatagoryFolderDir = new DirectoryInfo(catagoryFolderDir + "/" + subCatagoryFolderName);
if (!subCatagoryFolderDir.Exists)
{
subCatagoryFolderDir.Create();
}
}
}
}
示例3: CreateFolder
public static void CreateFolder(string path)
{
DirectoryInfo di = new DirectoryInfo(path);
if (di.Exists == false) {
di.Create();
}
}
示例4: OverwriteFile
// Copy or overwrite destination file with source file.
public static bool OverwriteFile(string srcFilePath, string destFilePath)
{
FileInfo fi = new FileInfo(srcFilePath);
if ( ! fi.Exists )
{
UnityEngine.Debug.LogError(string.Format("Wwise: Failed to overwrite. Source is missing: {0}.", srcFilePath));
return false;
}
DirectoryInfo di = new DirectoryInfo(Path.GetDirectoryName(destFilePath));
if ( ! di.Exists )
{
di.Create();
}
const bool IsToOverwrite = true;
try
{
fi.CopyTo(destFilePath, IsToOverwrite);
}
catch (Exception ex)
{
UnityEngine.Debug.LogError(string.Format("Wwise: Error during installation: {0}.", ex.Message));
return false;
}
return true;
}
示例5: CopyDirectory
/// <summary>
/// Copy thư mục này đến thư mục khác
/// </summary>
/// <param name="ThuMucNguon"></param>
/// <param name="ThucMucDich"></param>
public static void CopyDirectory(DirectoryInfo ThuMucNguon, DirectoryInfo ThucMucDich)
{
try
{
if (!ThucMucDich.Exists)
{
ThucMucDich.Create();
}
FileInfo[] files = ThuMucNguon.GetFiles();
foreach (FileInfo file in files)
{
if ((File.Exists(System.IO.Path.Combine(ThucMucDich.FullName, file.Name))) == false)
{
file.CopyTo(Path.Combine(ThucMucDich.FullName, file.Name));
}
}
//Xử lý thư mục con
DirectoryInfo[] dirs = ThuMucNguon.GetDirectories();
foreach (DirectoryInfo dir in dirs)
{
string ThucMucDichDir = Path.Combine(ThucMucDich.FullName, dir.Name);
CopyDirectory(dir, new DirectoryInfo(ThucMucDichDir));
}
}
catch { }
}
示例6: btnUpload_Click
protected void btnUpload_Click(object sender, EventArgs e)
{
try
{
string fileName = FileUpload1.FileName;
string prePath = Request["PrePath"];
string savePath = Server.MapPath("~/" + prePath);
DirectoryInfo dinfo = new DirectoryInfo(savePath);
if (!dinfo.Exists) {
dinfo.Create();
}
//Response.Write(savePath);
if (FileUpload1.PostedFile.ContentLength > 10485760)
{
AgileFrame.Core.ScriptHelper.ResponseScript(Page, "closeBarWindow('" + pdivID + "');alert('上传的文件太大,不能超过10M。');", false);
}
else
{
FileUpload1.SaveAs(savePath + fileName);
string js = "AddFileToParent('" + fileName + "','" + pdivID + "');closeBarWindow('" + pdivID + "');";
AgileFrame.Core.ScriptHelper.ResponseScript(Page, js, false);
}
}
catch {
string js = "closeBarWindow('" + pdivID + "');";
AgileFrame.Core.ScriptHelper.ResponseScript(Page, js, false);
}
}
示例7: 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();
}
示例8: CopyTo
/// <summary>A DirectoryInfo extension method that copies to.</summary>
/// <exception cref="Exception">Thrown when an exception error condition occurs.</exception>
/// <param name="obj">The obj to act on.</param>
/// <param name="destDirName">Pathname of the destination directory.</param>
/// <param name="searchPattern">A pattern specifying the search.</param>
/// <param name="searchOption">The search option.</param>
public static void CopyTo(this DirectoryInfo obj, string destDirName, string searchPattern, SearchOption searchOption)
{
var files = obj.GetFiles(searchPattern, searchOption);
foreach (var file in files)
{
var outputFile = destDirName + file.FullName.Substring(obj.FullName.Length);
var directory = new FileInfo(outputFile).Directory;
if (directory == null)
{
throw new Exception("The directory cannot be null.");
}
if (!directory.Exists)
{
directory.Create();
}
file.CopyTo(outputFile);
}
// Ensure empty dir are also copied
var directories = obj.GetDirectories(searchPattern, searchOption);
foreach (var directory in directories)
{
var outputDirectory = destDirName + directory.FullName.Substring(obj.FullName.Length);
var directoryInfo = new DirectoryInfo(outputDirectory);
if (!directoryInfo.Exists)
{
directoryInfo.Create();
}
}
}
示例9: btn_Click
protected void btn_Click(object sender, EventArgs e)
{
DirectoryInfo dir = new DirectoryInfo("C:\\IlinkbayForder");
if (!dir.Exists)
dir.Create();
foreach (FileInfo file in dir.GetFiles())
{
file.Delete();
}
System.IO.FileStream fileReportHeader = new System.IO.FileStream(@"C:\\IlinkbayForder\\heReportHeader.rtf", System.IO.FileMode.Create);
heReportHeader.Export(HtmlEditorExportFormat.Rtf, fileReportHeader);
System.IO.FileStream fileReportFooter = new System.IO.FileStream(@"C:\\IlinkbayForder\\heReportFooter.rtf", System.IO.FileMode.Create);
heReportFooter.Export(HtmlEditorExportFormat.Rtf, fileReportFooter);
fileReportHeader.Close();
fileReportFooter.Close();
string rtfFooter = System.IO.File.ReadAllText(@"C:\\IlinkbayForder\\heReportFooter.rtf");
string rtfHeader = System.IO.File.ReadAllText(@"C:\\IlinkbayForder\\heReportHeader.rtf");
SqlHelper.ExecuteScalar( DataServices.ConnectString, "SY_exportsetting_Update", int.Parse(Session["ID"].ToString()), rtfHeader, heReportHeader.Html, rtfFooter, heReportFooter.Html, chkLandscape.Checked, cmbPagesKind.Value, txtFileName.Text);
foreach (FileInfo file in dir.GetFiles())
{
file.Delete();
}
dir.Delete();
CacheObject.buildCache_globalExportSetting();
}
示例10: CopyDirectory
public void CopyDirectory(DirectoryInfo source, DirectoryInfo destination)
{
try
{
if (!destination.Exists)
{
destination.Create();
}
// Copy all files.
FileInfo[] files = source.GetFiles();
foreach (FileInfo file in files)
{
file.CopyTo(Path.Combine(destination.FullName,
file.Name));
}
// Process subdirectories.
DirectoryInfo[] dirs = source.GetDirectories();
foreach (DirectoryInfo dir in dirs)
{
// Get destination directory.
string destinationDir = Path.Combine(destination.FullName, dir.Name);
// Call CopyDirectory() recursively.
CopyDirectory(dir, new DirectoryInfo(destinationDir));
}
}
catch(Exception ex)
{
ClientScript.RegisterStartupScript(GetType(), "Javascript", "javascript: error('Operation Failed'); ", true);
}
}
示例11: 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);
}
}
}
示例12: TempDirectory
public TempDirectory()
{
var f = Path.GetTempFileName();
var name = Path.GetFileNameWithoutExtension(f);
File.Delete(f);
Directory = new DirectoryInfo(Path.Combine(Path.GetTempPath(), name));
Directory.Create();
}
示例13: CheckURL
//判斷檔案上傳路徑是否存在,若不存在則建立
public static void CheckURL(string strPaht)
{
DirectoryInfo di = new DirectoryInfo(strPaht);
if (!di.Exists)
{
di.Create();
}
}
示例14: WindowsAttributeSetting
public void WindowsAttributeSetting(FileAttributes attr)
{
var test = new DirectoryInfo(GetTestFilePath());
test.Create();
Set(test.FullName, attr);
Assert.Equal(attr | FileAttributes.Directory, Get(test.FullName));
Set(test.FullName, 0);
}
示例15: UpLoadFile
public string UpLoadFile(HttpPostedFile inputFile, string filePath, string myfileName)
{
if (string.IsNullOrEmpty(filePath))
{
throw new ApplicationException("路径不能为空");
}
if (string.IsNullOrEmpty(myfileName))
{
throw new ApplicationException("文件名不能为空");
}
FileUpLoad fp = new FileUpLoad();
//建立上传对象
HttpPostedFile postedFile = inputFile;
FileName = System.IO.Path.GetFileName(postedFile.FileName);
FileExtension = System.IO.Path.GetExtension(FileName).ToUpper();
//如果格式都不符合则返回
if (!".JPG".Equals(FileExtension) && !".JPEG".Equals(FileExtension) && !".PNG".Equals(FileExtension) )
{
throw new ApplicationException("上传图片格式正确,请选择jpg,png格式的图片");
}
if (postedFile.ContentLength > 1024 * 1024 * 20)
{
throw new ApplicationException("最大只能上传20M文件");
}
if (myfileName != string.Empty)
{
FileName = myfileName+FileExtension;
}
string phyPath = HttpContext.Current.Server.MapPath("upload");
//判断路径是否存在,若不存在则创建路径
DirectoryInfo upDir = new DirectoryInfo(phyPath);
if (!upDir.Exists)
{
upDir.Create();
}
try
{
string tempPath = Path.Combine(phyPath, filePath);
string originalImagePath = Path.Combine(tempPath, FileName);
string thumbnailPath = Path.Combine(tempPath,"thumbnail_" + FileName);
//保存源文件
postedFile.SaveAs(originalImagePath);
ImageHelper.MakeThumbnail(originalImagePath, thumbnailPath,80,80,"W");
}
catch
{
throw new ApplicationException("上传失败!");
}
return "/upload/" + filePath + "/thumbnail_" + FileName;
}