Showing posts with label WPF. Show all posts
Showing posts with label WPF. Show all posts

Wednesday

Create Folder on Desktop in ASP.NET

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