Directory and DirectoryInfo Classes can be used to get some information or for doing some manipulation regarding Directory.This classes are basically used for Directory Manipulation.
Step :2 Now Double click on Submit Button and write the following codes which is given below:
OUTPUT:
Description:-Here i have entered Existing Directory(os) and click the submit button ,then sub directory under Directory(os) will be showed in the comboBox .
Note:- You can use Asp.NET Empty Website instead of Windows Forms Application. Both can be used to perform File handling concepts.
Example of DirectoryInfo Class:- There are also some steps please follow it which is given below:- Directory Class Provide Static Method.
- DirectoryInfo class provide Non Static Method.
Step :1 First open Your Visual studio--> File-->New-->Project->Windows Forms Application-->OK.--> Drag and Drop controls on form like given below:
Step :2 Now Double click on Submit Button and write the following codes which is given below:
using System;
using System.IO;
using System.Windows.Forms;
namespace Directory_class
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (Directory.Exists(textBox1.Text))
{
String[] sfile = Directory.GetFiles(textBox1.Text);
foreach (String s in sfile)
{
comboBox1.Items.Add(s);
}
}
else
{
Directory.CreateDirectory(textBox1.Text);
MessageBox.Show("Directory created");
}
}
}
}
Step :3 Now Run the Program(Press F5).OUTPUT:
Description:-Here i have entered Existing Directory(os) and click the submit button ,then sub directory under Directory(os) will be showed in the comboBox .
Note:- You can use Asp.NET Empty Website instead of Windows Forms Application. Both can be used to perform File handling concepts.
Step :1 First open Your Visual studio--> File-->New-->Project->Windows Forms Application-->OK.--> Drag and Drop controls on form like given below:
Step :2 Now Double click on Submit Button and write the following codes which is given below:
using System;
using System.IO;
using System.Windows.Forms;
namespace Directoryinfo_class
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
DirectoryInfo dinfo = new DirectoryInfo(textBox1.Text);
if (dinfo.Exists)
{
FileInfo[] sinfo = dinfo.GetFiles();
foreach (FileInfo s in sinfo)
{
comboBox1.Items.Add(s);
}
}
else
{
dinfo.Create();
MessageBox.Show(" New Directory has been created");
}
}
}
}
Step :3 Now Run the Program(Press F5). Output:
Description:- In above Example i have created a New Directory Using DirectoryInfo class in D drive. In this program i have created the object of DirectoryInfo class because DirectoryInfo class provide Non Static Method.So we need to create the object of DirectoryInfo class.
Note:- You can use Asp.NET Empty Website instead of Windows Forms Application. Both can be used to perform File handling concepts.
For more
I Hope this is helpful for you.
To Get the Latest Free Updates Subscribe
Click below for download whole application.
Download
This is a good tutorial. I have a quick question. What are the scenarios where I should Directory and DirectoryInfo classes? Any real time examples would be a great help
ReplyDelete