我在我的应用程序中使用了 FileUploader
控件。我想将文件保存到指定的文件夹。如果此文件夹不存在,我想先创建它,然后将我的文件保存到此文件夹中。如果文件夹已经存在,那么只需将文件保存在其中。
我怎样才能做到这一点?
正如其他人所说,使用 System.IO.Directory.CreateDirectory
。
但是,您不需要先检查它是否存在。从 the documentation:
除非它们已经存在或路径的某些部分无效,否则会创建路径中指定的任何和所有目录。如果目录已存在,则此方法不会创建新目录,但会返回现有目录的 DirectoryInfo 对象。
根据 How can I create a folder dynamically using the File upload server control? 使用以下代码:
string subPath ="ImagesPath"; // Your code goes here
bool exists = System.IO.Directory.Exists(Server.MapPath(subPath));
if(!exists)
System.IO.Directory.CreateDirectory(Server.MapPath(subPath));
FileExists
(或任何 C# 等效项)异常,以防函数旨在抛出异常。
Exists
,它实际上会创建一个新的失败条件。
System.IO.Directory.CreateDirectory
的 API 是什么的错误印象。 (而且它更快,但可能没关系)
只需写下这一行:
System.IO.Directory.CreateDirectory("my folder");
如果该文件夹尚不存在,则会创建该文件夹。
如果文件夹已经存在,则该行将被忽略。
参考:Article about Directory.CreateDirectory at MSDN
当然,您也可以在源文件顶部写 using System.IO;
,然后每次要创建文件夹时只写 Directory.CreateDirectory("my folder");
。
如果路径尚不存在,您可以使用如下方法创建路径:
using System.IO;
private void CreateIfMissing(string path)
{
bool folderExists = Directory.Exists(Server.MapPath(path));
if (!folderExists)
Directory.CreateDirectory(Server.MapPath(path));
}
if (!folderExists)
。
FileExists
异常(或在 C# 中调用的任何异常)
Directory.CreateDirectory
解释了如何尝试并在 FilePath 不存在时创建它。
Directory.Exists
说明如何检查 FilePath 是否存在。但是,您不需要它,因为 CreateDirectory 会为您检查它。
您可以使用 try/catch 子句并检查它是否存在:
try
{
if (!Directory.Exists(path))
{
// Try to create the directory.
DirectoryInfo di = Directory.CreateDirectory(path);
}
}
catch (IOException ioex)
{
Console.WriteLine(ioex.Message);
}
如果文件夹不存在,此方法将创建该文件夹,如果存在则不执行任何操作:
Directory.CreateDirectory(path);
using System.IO
if (!Directory.Exists(yourDirectory))
Directory.CreateDirectory(yourDirectory);
if (!Directory.Exists(Path.GetDirectoryName(fileName)))
{
Directory.CreateDirectory(Path.GetDirectoryName(fileName));
}
以下代码是我使用的最好的代码行,如果不存在,它将创建目录。
System.IO.Directory.CreateDirectory(HttpContext.Current.Server.MapPath("~/temp/"));
如果目录已存在,则此方法不会创建新目录,但会返回现有目录的 DirectoryInfo 对象。 >
给定父文件夹的路径,创建一个新文件夹:
string pathToNewFolder = System.IO.Path.Combine(parentFolderPath, "NewSubFolder");
DirectoryInfo directory = Directory.CreateDirectory(pathToNewFolder);
// Will create if does not already exist (otherwise will ignore)
给定新文件夹的路径
目录信息变量,以便您可以继续随意操作它。
如果文件夹不在图像文件夹或其他文件夹下,请使用此代码
string subPath = HttpContext.Current.Server.MapPath(@"~/Images/RequisitionBarCode/");
bool exists = System.IO.Directory.Exists(subPath);
if(!exists)
System.IO.Directory.CreateDirectory(subPath);
string path = HttpContext.Current.Server.MapPath(@"~/Images/RequisitionBarCode/" + OrderId + ".png");
使用下面的代码。我使用此代码进行文件复制和创建新文件夹。
string fileToCopy = "filelocation\\file_name.txt";
String server = Environment.UserName;
string newLocation = "C:\\Users\\" + server + "\\Pictures\\Tenders\\file_name.txt";
string folderLocation = "C:\\Users\\" + server + "\\Pictures\\Tenders\\";
bool exists = System.IO.Directory.Exists(folderLocation);
if (!exists)
{
System.IO.Directory.CreateDirectory(folderLocation);
if (System.IO.File.Exists(fileToCopy))
{
MessageBox.Show("file copied");
System.IO.File.Copy(fileToCopy, newLocation, true);
}
else
{
MessageBox.Show("no such files");
}
}
一种奇特的方法是使用您想要的方法扩展 FileUpload
。
添加这个:
public static class FileUploadExtension
{
public static void SaveAs(this FileUpload, string destination, bool autoCreateDirectory) {
if (autoCreateDirectory)
{
var destinationDirectory = new DirectoryInfo(Path.GetDirectoryName(destination));
if (!destinationDirectory.Exists)
destinationDirectory.Create();
}
file.SaveAs(destination);
}
}
然后使用它:
FileUpload file;
...
file.SaveAs(path,true);
FileUploadExtension
不在任何地方使用(?)。
string root = @"C:\Temp";
string subdir = @"C:\Temp\Mahesh";
// If directory does not exist, create it.
if (!Directory.Exists(root))
{
Directory.CreateDirectory(root);
}
CreateDirectory 也用于创建子目录。您所要做的就是指定将在其中创建此子目录的目录的路径。以下代码片段在 C:\Temp directory
中创建一个 Mahesh 子目录。
// Create sub directory
if (!Directory.Exists(subdir))
{
Directory.CreateDirectory(subdir);
}
从多个答案派生/组合,为我实现它就像这样简单:
public void Init()
{
String platypusDir = @"C:\platypus";
CreateDirectoryIfDoesNotExist(platypusDir);
}
private void CreateDirectoryIfDoesNotExist(string dirName)
{
System.IO.Directory.CreateDirectory(dirName);
}
不定期副业成功案例分享