Introduction
Today, I am also demonstrating another example, how to create folder on Desktop in C# in WPF (C#) application. Earlier, I have already written on How to write in Text file on desktop in vb.net. When we are interacting C# application and we want to create a folder on desktop then this article will help you.
Create Folder on Desktop
Get Desktop Path
Here I am collecting desktop folder through method “Environment.SpecialFolder.DesktopDirectory”
1
2
string strDestopPath = System.Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
Following Namespace Used
1
2
using System.IO;
Create Folder Button
Validate Folder is created on Desktop or Not
If folder is not created on desktop then it will create otherwise it will skip to create a new folder on Desktop.
1
2
if (!System.IO.Directory.Exists(strDestopPath))
Create Folder on Desktop
DirectoryInfo object is used to create folder on desktop.
1
2
DirectoryInfo di = System.IO.Directory.CreateDirectory(strDestopPath);
Desktop view
Desktop view
C# WPF Code Snippet:
private void button1_Click(object sender, RoutedEventArgs e)
{
string strDestopPath = System.Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
strDestopPath += "\Downloads";
if (!System.IO.Directory.Exists(strDestopPath))
{
DirectoryInfo di = System.IO.Directory.CreateDirectory(strDestopPath);
MessageBox.Show("Successfully Created");
}
else
{
MessageBox.Show("Already Created!");
}
strDestopPath += "\New Download";
if (!System.IO.Directory.Exists(strDestopPath))
{
DirectoryInfo di = System.IO.Directory.CreateDirectory(strDestopPath);
}
}
private void button1_Click(object sender, RoutedEventArgs e)
{
string strDestopPath = System.Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
strDestopPath += "\Downloads";
if (!System.IO.Directory.Exists(strDestopPath))
{
DirectoryInfo di = System.IO.Directory.CreateDirectory(strDestopPath);
MessageBox.Show("Successfully Created");
}
else
{
MessageBox.Show("Already Created!");
}
strDestopPath += "\New Download";
if (!System.IO.Directory.Exists(strDestopPath))
{
DirectoryInfo di = System.IO.Directory.CreateDirectory(strDestopPath);
}
}
New Download Created
Summary
This program helps us to create folder on desktop whenever required in our Windows/Desktop Application. This example runs successfully in Windows Application (normal), WPF, Win Forms etc.
visit our site-https://www.technologycrowds.com/2011/04/create-folder-on-desktop-in-aspnet.html
Top comments (0)