Navigation

Create Folder on Desktop in ASP.NET

create folder in c#, create folder on desktop c#, Environment.SpecialFolder.DesktopDirectory,

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”
string strDestopPath = System.Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);

Following Namespace Used

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.
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);

 
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);
         }
}


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.

Suggested Reading

Share

Anjan Kant

Outstanding journey in Microsoft Technologies (ASP.Net, C#, SQL Programming, WPF, Silverlight, WCF etc.), client side technologies AngularJS, KnockoutJS, Javascript, Ajax Calls, Json and Hybrid apps etc. I love to devote free time in writing, blogging, social networking and adventurous life

Post A Comment:

4 comments:

  1. Might be required permission, it depends upon permission granted to users by administrator so that confirm permission to create folder on desktop.

    ReplyDelete