Thursday, 5 March 2020

Create directory using command Prompt / batch file in c# windows services






        public void Create_BatFile_ForDirectories()
        {
            string mDrive = "C:", /////// Drive where want to create file
                mFolder = "\\WindowService\\Service", //// Directory folders names
                BatFileName = "Test.bat";       //// Batch file name 'you can create folder with this batch file
            string ProgramPath = mDrive + mFolder;
            try
            {               
                List<string> list = new List<string>
                    {
                        mDrive,
                        "dir " + mFolder
                      };
                File.WriteAllLines(mDrive + "\\" + BatFileName, list.ToArray()); /// create batch file in C drive

            }
            catch (Exception ex)
            {
            }
            Execute_Process(ProgramPath + "\\" + BatFileName); //// Execute batch file with process to create directory
        }

        public void Create_BatFile_ForListOfFiles()
        {
            string mDrive = "C:",  //// Drive where want to create file
                mFolder = "\\WindowService\\Service", //// Directory folders names
                mTxtFile = "Test.txt", //// Name of the text file where you want to list of the files inside the DIR
                BatFileName = "Testbatch.bat";   //// Batch file name 'you can create folder with this batch file

            string ProgramPath = mDrive + mFolder;
            try
            {
                List<string> list = new List<string>
                    {
                        mDrive,
                        "cd" + mFolder,
                        "dir *.* /s/b >>" + mTxtFile
                      };
                  //// create batch file in C drive
                File.WriteAllLines(mDrive + "\\" + BatFileName, list.ToArray());
            }
            catch (Exception ex)
            {
            }
          //// Execute batch file with process to create list of file inside the DIR
            Execute_Process(ProgramPath + "\\" + BatFileName); 
        }

        public void Execute_Process(string ProcessBatFileFullPathWithName)
        {
            try
            {
                Process prsCopy = new Process();
                prsCopy.StartInfo.RedirectStandardError = true;
                prsCopy.StartInfo.RedirectStandardInput = true;
                prsCopy.StartInfo.RedirectStandardOutput = true;
                prsCopy.StartInfo.UseShellExecute = false;
                prsCopy.StartInfo.CreateNoWindow = true;
                prsCopy.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                prsCopy.StartInfo.FileName = ProcessBatFileFullPathWithName;
                prsCopy.Start();
                prsCopy.WaitForExit();

            }
            catch (Exception ex)
            {
            }
        }

No comments:

Post a Comment

How to highlight selected text in notepad++

  –> To highlight a block of code in Notepad++, please do the following steps step-1  :- Select the required text. step-2  :- Right click...