Tuesday 22 July 2014

useFull code for creating Directory and Folder

private void btnBrowseLocation_Click(object sender, EventArgs e)
        {
            try
            {
                tbMwSwat.SelectedIndex = 0;
                string fileName = string.Empty;
                //SaveFileDialog sfdFileName = new SaveFileDialog();
                //sfdFileName.Filter = "Text File|*.txt";
                //sfdFileName.Title = "Save File";
                //DialogResult dlrFileName = sfdFileName.ShowDialog();
                FolderBrowserDialog fbdRIBASIM = new FolderBrowserDialog();
                fbdRIBASIM.Description = "Browse Saving Location";
                DialogResult dlrFileName = fbdRIBASIM.ShowDialog();
                if (!dlrFileName.Equals(DialogResult.Cancel))
                {
                    fileName = fbdRIBASIM.SelectedPath;// sfdFileName.FileName;
                    string directoryName = Path.GetDirectoryName(fileName);
                    string strOnlyFileName = string.Empty;

                    strOnlyFileName = fileName.Replace(directoryName, "");
                    // string strAfterRemovingLast4Char = strOnlyFileName.Remove(strOnlyFileName.Length - 4,4);
                    string pathName = directoryName;
                    string pathRainfallFolder = fileName + "\\TS\\";//pathName +"\\"+strAfterRemovingLast4Char+ "\\TS\\";
                    if (!Directory.Exists(pathRainfallFolder))
                    {
                        Directory.CreateDirectory(pathRainfallFolder);
                    }
                    //txtSavingLocation.Text = pathRainfallFolder + strOnlyFileName;
                    txtSavingLocation.Text = fileName;//pathName + "\\" + strAfterRemovingLast4Char;
                    _strTxtSavingLocation = pathRainfallFolder + strOnlyFileName + ".txt";
                    //FileStream fsWRAPTIN = new FileStream(txtSavingLocation.Text, FileMode.Create, FileAccess.Write);
                    FileStream fsWRAPTIN = new FileStream(_strTxtSavingLocation, FileMode.Create, FileAccess.Write);
                    StreamWriter swWRAPTIN = new StreamWriter(fsWRAPTIN, Encoding.UTF8);

                    swWRAPTIN.Flush();
                    swWRAPTIN.Close();
                    fsWRAPTIN.Dispose();

                    //MessageBox.Show("DAT file created successfully.");
                    // objUIMethod.MsgShow("iMsg1153", GlobalResource.Message.iMsg1153);
                }
            }
            catch (Exception ex)
            {
                objUIMethod.MsgShow("eMsg", ex.Message);
                LoggerClass.ErrorException(ex.Message, ex);
            }

No comments:

Post a Comment