जवाबों:
using System.IO;
...
Directory.CreateDirectory(@"C:\MP_Upload");
Directory.CreateDirectory वही करती है जो आप चाहते हैं: यह निर्देशिका बनाता है यदि यह अभी तक मौजूद नहीं है। पहले एक स्पष्ट जाँच करने की कोई आवश्यकता नहीं है।
पथ में निर्दिष्ट कोई भी और सभी निर्देशिका तब तक बनाई जाती हैं, जब तक कि वे पहले से मौजूद न हों या जब तक कि पथ का कुछ हिस्सा अमान्य न हो। पथ पैरामीटर निर्देशिका पथ निर्दिष्ट करता है, न कि फ़ाइल पथ। यदि निर्देशिका पहले से मौजूद है, तो यह विधि कुछ भी नहीं करती है।
(इसका मतलब यह भी है कि यदि आवश्यक हो तो पथ के सभी निर्देशिकाएं बनाई जाती हैं: CreateDirectory(@"C:\a\b\c\d")पर्याप्त है, भले ही C:\aअभी तक मौजूद नहीं है।)
मुझे अपनी पसंद की निर्देशिका के बारे में सावधानी से एक शब्द जोड़ने दें, हालाँकि: सिस्टम विभाजन रूट के ठीक नीचे एक फ़ोल्डर बनाना C:\उस पर आधारित है। उपयोगकर्ता को एक फ़ोल्डर चुनने %APPDATA%या उसके %LOCALAPPDATA%बजाय एक फ़ोल्डर बनाने पर विचार करने के लिए ( पर्यावरण का उपयोग करें । उसके लिए फ़ोल्डर फ़ोल्डर )। Environment.SpecialFolder गणन का MSDN पृष्ठ विशेष ऑपरेटिंग सिस्टम फ़ोल्डर्स और उनके उद्देश्यों की एक सूची शामिल करता है।
EnsureDirectoryExistsने विधि को खोजने के लिए कठिन बना दिया होगा।
Directory.CreateDirectoryअगर फ़ोल्डर का नाम एक मौजूदा फ़ाइल नाम से मेल खाता है तो फेंक देगा।
if(!System.IO.Directory.Exists(@"c:\mp_upload"))
{
System.IO.Directory.CreateDirectory(@"c:\mp_upload");
}
Createकरने के लिए CreateDirectory:)
using System;
using System.IO;
using System.Windows.Forms;
namespace DirCombination
{
public partial class DirCombination : Form
{
private const string _Path = @"D:/folder1/foler2/folfer3/folder4/file.txt";
private string _finalPath = null;
private string _error = null;
public DirCombination()
{
InitializeComponent();
if (!FSParse(_Path))
Console.WriteLine(_error);
else
Console.WriteLine(_finalPath);
}
private bool FSParse(string path)
{
try
{
string[] Splited = path.Replace(@"//", @"/").Replace(@"\\", @"/").Replace(@"\", "/").Split(':');
string NewPath = Splited[0] + ":";
if (Directory.Exists(NewPath))
{
string[] Paths = Splited[1].Substring(1).Split('/');
for (int i = 0; i < Paths.Length - 1; i++)
{
NewPath += "/";
if (!string.IsNullOrEmpty(Paths[i]))
{
NewPath += Paths[i];
if (!Directory.Exists(NewPath))
Directory.CreateDirectory(NewPath);
}
}
if (!string.IsNullOrEmpty(Paths[Paths.Length - 1]))
{
NewPath += "/" + Paths[Paths.Length - 1];
if (!File.Exists(NewPath))
File.Create(NewPath);
}
_finalPath = NewPath;
return true;
}
else
{
_error = "Drive is not exists!";
return false;
}
}
catch (Exception ex)
{
_error = ex.Message;
return false;
}
}
}
}
String path = Server.MapPath("~/MP_Upload/");
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
आप यह कोशिश कर सकते हैं ..
using System.IO;string path = "C:\MP_Upload";if(!Directory.Exists(path)){
Directory.CreateDirectory(path);}