Using The Directory class
The Directory class exposes static methods to create ,move and enumerate directories and subdirectories. This class is located under the System.IO namespace and is not inherited by any other class. Using the Directory class, we can accomplish a number of operations, such as moving a directory from one location to another and remaining a directory. We do not need to create objects of the Directory class because this class provides static methods. A drawback of the Directory class is that it takes longer time to execute, since every time we call a method, The Directory class performs a security check.
Take a Example of Create a new Directory
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApplication2
{
public partial class Form4 : Form
{
public Form4()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string path = @"c:\" + textBox1.Text;
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
label2 .Text =@"You directory has baan created in c:\";
}
else
{
if (Directory.Exists(path))
{
label2.Text = @"You directory already exist in c:\";
}
}
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApplication2
{
public partial class Form4 : Form
{
public Form4()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string path = @"c:\" + textBox1.Text;
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
label2 .Text =@"You directory has baan created in c:\";
}
else
{
if (Directory.Exists(path))
{
label2.Text = @"You directory already exist in c:\";
}
}
}
}
}
Tidak ada komentar:
Posting Komentar