Uso un controllo FileUploader nella mia applicazione. Voglio salvare un file in una cartella specificata. Ora voglio, se questa cartella non esiste, per prima cosa creala e poi salva il mio file in questa cartella. Se la cartella esiste già, basta salvare il file al suo interno.
Come posso farlo?
Come altri hanno già detto, usa System.IO.Directory.CreateDirectory
Ma non è necessario controllare se esiste prima. Da docs
Qualsiasi e tutte le directory specificate nel percorso vengono create, a meno che non esistano già o a meno che alcune parti del percorso non siano valide. Se la directory esiste già, questo metodo non crea una nuova directory, ma restituisce un oggetto DirectoryInfo per la directory esistente.
Usa il codice seguente come http://forums.asp.net/p/1226236/2209871.aspx :
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));
Basta scrivere questa riga:
System.IO.Directory.CreateDirectory("my folder");
Riferimento: Articolo su Directory.CreateDirectory su MSDN
Naturalmente, puoi anche scrivere using System.IO;
nella parte superiore del file sorgente e quindi scrivere Directory.CreateDirectory("my folder");
ogni volta che vuoi creare una cartella.
Puoi creare il percorso se non esiste ancora con un metodo come il seguente:
using System.IO;
private void CreateIfMissing(string path)
{
bool folderExists = Directory.Exists(Server.MapPath(path));
if (!folderExists)
Directory.CreateDirectory(Server.MapPath(path));
}
Directory.Exists
Questo spiegherà come verificare se esiste un FilePath
Directory.CreateDirectory
Questo spiegherà come provare e creare il FilePath se non esiste
Puoi utilizzare una clausola try/catch e controllare se esiste:
try
{
if (!Directory.Exists(path))
{
// Try to create the directory.
DirectoryInfo di = Directory.CreateDirectory(path);
}
}
catch (IOException ioex)
{
Console.WriteLine(ioex.Message);
}
using System.IO
if (!Directory.Exists(yourDirectory))
Directory.CreateDirectory(yourDirectory);
Questo metodo creerà la cartella se non esiste e non farà nulla se esiste
Directory.CreateDirectory(path);
if (!Directory.Exists(Path.GetDirectoryName(fileName)))
{
Directory.CreateDirectory(Path.GetDirectoryName(fileName));
}
Il seguente codice è la migliore linea (s) di codice che uso che creerà la directory se non presente.
System.IO.Directory.CreateDirectory(HttpContext.Current.Server.MapPath("~/temp/"));
Se la directory esiste già, questo metodo non crea una nuova directory, ma restituisce un oggetto DirectoryInfo per la directory esistente. >
Questa era la risposta che stavo cercando, ma non trovai prontamente:
string pathToNewFolder = System.IO.Path.Combine(parentFolderPath, "NewSubFolder");
DirectoryInfo directory = Directory.CreateDirectory(pathToNewFolder);
// Will create if does not already exist (otherwise will ignore)
Usa sotto il codice. Ho usato questo codice per copiare i file e creare una nuova cartella.
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");
}
}
string createfolder = "E:/tmp /" + uId;
System.IO.Directory.CreateDirectory (CreateFolder);