Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 357 Vote(s) - 3.45 Average
  • 1
  • 2
  • 3
  • 4
  • 5
If a folder does not exist, create it

#1
I use a `FileUploader` control in my application. I want to save a file to a specified folder. If this folder does not exist, I want to first create it, and then save my file to this folder. If the folder already exists, then just save the file in it.

How can I do this?
Reply

#2
using System.IO

if (!Directory.Exists(yourDirectory))
Directory.CreateDirectory(yourDirectory);
Reply

#3
You can create the path if it doesn't exist yet with a method like the following:

using System.IO;

private void CreateIfMissing(string path)
{
bool folderExists = Directory.Exists(Server.MapPath(path));
if (!folderExists)
Directory.CreateDirectory(Server.MapPath(path));
}
Reply

#4
string root = @"C:\Temp";

string subdir = @"C:\Temp\Mahesh";

// If directory does not exist, create it.

if (!Directory.Exists(root))
{

Directory.CreateDirectory(root);

}

The CreateDirectory is also used to create a sub directory. All you have to do is to specify the path of the directory in which this subdirectory will be created in. The following code snippet creates a Mahesh subdirectory in `C:\Temp directory`.


// Create sub directory

if (!Directory.Exists(subdir))
{

Directory.CreateDirectory(subdir);

}
Reply

#5
Derived/combined from multiple answers, implementing it for me was as easy as this:

public void Init()
{
String platypusDir = @"C:\platypus";
CreateDirectoryIfDoesNotExist(platypusDir);
}

private void CreateDirectoryIfDoesNotExist(string dirName)
{
System.IO.Directory.CreateDirectory(dirName);
}

Reply

#6
if (!Directory.Exists(Path.GetDirectoryName(fileName)))
{
Directory.CreateDirectory(Path.GetDirectoryName(fileName));
}
Reply

#7
You can use a try/catch clause and check to see if it exist:

try
{
if (!Directory.Exists(path))
{
// Try to create the directory.
DirectoryInfo di = Directory.CreateDirectory(path);
}
}
catch (IOException ioex)
{
Console.WriteLine(ioex.Message);
}
Reply

#8
A fancy way is to extend the `FileUpload` with the method you want.

Add this:

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);
}
}

Then use it:

FileUpload file;
...
file.SaveAs(path,true);
Reply

#9
Use the below code as per *[How can I create a folder dynamically using the File upload server control?][1]*:

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));

[1]:

[To see links please register here]


Reply

#10
[`Directory.CreateDirectory`][2] explains how to try and to create the FilePath if it does not exist.

[`Directory.Exists`][1] explains how to check if a FilePath exists. However, you don't need this as CreateDirectory will check it for you.

[1]:

[To see links please register here]

[2]:

[To see links please register here]



Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through