Wednesday 4 December 2013

How to get server path and how to create Directory in C#


In this article I will explain how to get server path and how to create Directory in C#.net

Write below for server path for specific folder.

string Images_Path = Server.MapPath("~/") + "\\Upload\\";

How to create Directory in C#

First we need to add below namespace in class file.

using System.IO;



The below method is useful for create Directory

  // Creating Directory for finding Server path
        public DirectoryInfo CreateUserDirectory(string directoryPath)
        {
            string userName = System.Guid.NewGuid().ToString("N");
            if (!string.IsNullOrEmpty(User.Identity.Name))
            {
                userName = User.Identity.Name;
            }
            string userDirectoryName = directoryPath + "\\" + userName;
            DirectoryInfo di;

            if (!Directory.Exists(userDirectoryName))
            {
                di = System.IO.Directory.CreateDirectory(userDirectoryName);
                return di;
            }
            else
            {
                di = new DirectoryInfo(userDirectoryName);
            }
            return di;
        }



No comments:

Post a Comment