Hi, I'm trying to create folder name (serialno) from user input (GUI) in textbox and DataMonitoring and DataControlling output files will be save in the created folder.

        public bool DataMonitoring(ref string returnmsg)
        {
            string folderName = String.Format(@"C:\DataOutput\{0}", serialno);  
            String DateAndTime = DateTime.Now.ToString("yyyy-MM-dd_HH.mm.ss");
            FilePath = DateAndTime + "_DataMonitoring";

        }

        public bool DataControlling(ref string returnmsg)
        {
            string folderName = String.Format(@"C:\DataOutput\{0}", serialno);  
            String DateAndTime = DateTime.Now.ToString("yyyy-MM-dd_HH.mm.ss");
            FilePath = DateAndTime + "_DataControlling";

        }

Alright, so you have the filename as well as the folder name. I am going to assume you got this far, but you are unsure where to go next?

Well if that is the case, first of all, make sure you have the following reference using System.IO;

Now when I create programs that need to save to a specific filepath, and all I have is the name of the file I want to save, I used the following piece of code

if (Directory.Exists(Path.GetDirectoryName(Filename)) == false) 
{
    Directory.CreateDirectory(Path.GetDirectoryName(Filename));
}

That piece of code assumes that the "Filename" variable contains the name of the file as well, why I have to use the Path.GetDirectoryName(). If you know your folder path, then you can take that part out.

That small piece of code will create the directory you need (and I am pretty sure will actually create all the folders you need, like if two folders mentioned in the path don't exist ... BUT I can't remember for sure, you may want to check it).

Is this what you were looking for? You gave us a kind of cryptic message for help

Edited 2 Years Ago by JOSheaIV

This article has been dead for over six months. Start a new discussion instead.