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.Get Desktop Path
Here I am collecting desktop folder through method “Environment.SpecialFolder.DesktopDirectory”
string strDestopPath = System.Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
Following Namespace Used
using System.IO;
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.
if (!System.IO.Directory.Exists(strDestopPath))
Create Folder on Desktop
DirectoryInfo object is used to create folder on desktop.
DirectoryInfo di = System.IO.Directory.CreateDirectory(strDestopPath);
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); } }
thanks! Keep reading...
ReplyDeleteWill it be any permission needed?
ReplyDeleteASP.NET Training
Might be required permission, it depends upon permission granted to users by administrator so that confirm permission to create folder on desktop.
ReplyDelete