Click here to Skip to main content
15,922,155 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello


Please tell me this code is write or not, Please help me with correct code.

Please help me with this, i have following code for putting all my csv files in d drive. They go in d drive as folder,(19_06_12). Now i want to create another folder contract which consist of sub folder with name as 190612, keeping previous code as well as new code also.

1. Previous folder D:\Transaction\19_06_12\

2. Now i want this folder also D:\Transaction\Contract\190612\

But kepping previous folder also.

Please can some one give me the code for this.

C#
<public void="" mode="hold" />    {
        string Day = Convert.ToString(DateTime.Today.Day);
        string Month = Convert.ToString(DateTime.Today.Month);

        if (Day.Length == 1)
        {
            Day = "0" + Day;
        }

        if (Month.Length == 1)
        {
            Month = "0" + Month;
        }

        

        string path = @"D:\TransactionRequest\" + Day + "_" + Month + "_" + DateTime.Today.Year;

        
        //string path = @"D:\TransactionRequest\" + DateTime.Today.Day + "_" + DateTime.Today.Month + "_" + DateTime.Today.Year;
        try
        {
            if (!(Directory.Exists(path)))
            {
                DirectoryInfo di = Directory.CreateDirectory(path);
            }
        }
        catch (Exception ex)
        {

        }
        path = string.Empty;

        path = @"D:\TransactionRequest\ContractMaster" + Day + Month + DateTime.Today.Year;

        try
        {
            if (!(Directory.Exists(path)))
            {
                DirectoryInfo di = Directory.CreateDirectory(path);
            }
        }
        catch (Exception ex)
        {

        }
    }

Please help.
Posted
Updated 19-Jun-12 23:29pm
v4
Comments
OriginalGriff 19-Jun-12 7:17am    
What part of it is giving you problems?
Nikil0012 19-Jun-12 7:20am    
I want to create another string path so i want to ask you a question that can i write a code using string path1 and keeping string path as it is.

use these line in your code after adding one folder that

C#
path="";
        path = @"D:\TransactionRequest\" + Day +  Month + DateTime.Today.Year;

    try
        {
            if (!(Directory.Exists(path)))
            {
                DirectoryInfo di = Directory.CreateDirectory(path);
            }
        }
        catch (Exception ex)
        {

        }
 
Share this answer
 
Hello Nikil0012

I re-written some of your code hope you don't mind :)

I think their are two possible solutions of your problem. all the solutions are depending on the situation.

I've tried to explain the code by addin some comment. If you have any questions don't hesitate to post them.

C#
public void test()
       {
           string day = DateTime.Today.Day.ToString().PadLeft(2,'0');
           string month = DateTime.Today.Month.ToString().PadLeft(2,'0');

           //First solution: This solution is preferrable when you don't need the paths futher down the code.
           string path = string.Format(@"D:\TransactionRequest\{0}_{1}_{2}", day, month, DateTime.Now.Year);
           CheckOrCreateFolder(path);
           path = string.Format(@"D:\TransactionRequest\ContractMaster{0}_{1}_{2}", day, month, DateTime.Now.Year);
           CheckOrCreateFolder(path);
           //Second solution: I would choose this solution if you are going to use the variable futher down
           // and you don't want to loose or re-assign the variable
           string path1 = string.Format(@"D:\TransactionRequest\{0}_{1}_{2}", day, month, DateTime.Now.Year);
           string path2 = string.Format(@"D:\TransactionRequest\ContractMaster{0}_{1}_{2}", day, month, DateTime.Now.Year);
           CheckOrCreateFolder(path1);
           CheckOrCreateFolder(path2);


       }


And the extra method

C#
public void CheckOrCreateFolder(string path)
       {
           try
           {
               if (!(Directory.Exists(path)))
               {
                   Directory.CreateDirectory(path);
               }
           }
           //Catch block isn't necessary if you don't do anything with the exception
           catch (Exception)
           {
               throw;
           }
       }
 
Share this answer
 
v2
Also possible:
C#
protected void CheckMyFolders()
{
        List<String> Folders = GetMyFolders();
        try
        {
			foreach (string folder in Folders)
			{
				if (!(Directory.Exists(folder)))
				{
					DirectoryInfo di = Directory.CreateDirectory(folder);
				}
			}
        }
        catch (Exception ex)
        {

        }
 }

private  List<String> GetMyFolders()
{
    List<String> Folders = new List<String>();
    Folders.Add(string.Format(@"D:\TransactionRequest\{0}", DateTime.Now.ToString("dd_MM_yyyy")));
    Folders.Add(string.Format(@"D:\TransactionRequest\ContractMaster{0}", DateTime.Now.ToString("ddMMyyyy")));

    return Folders();
}
 
Share this answer
 
Hi,
As the path where folders need to be created are constant you can declare both the strings as static variables.
Using the above post you can create the folder

Thanks
www.alacraft.com.au
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900