Developer
Developer

Reputation: 8636

Is it posible to save file with the desired name I choose

Hi all i will have some file name to be saved with the name i choose . Like when i click on save on my form i will show a save dialog option and on the file name of that window i would like to have the filename of my own like some or other name and when he clicks on save i would like to save that file...

ANy idea..

Actually i have written a code to save my file as follows

        public bool savePPD(string strPath)
    {
        m_flag = true;
        string FileName = strPath;
        string m_strDate = DateTime.Now.ToString("MM/dd/yyyy");
        m_strDate = m_strDate.Replace("/", "");
        strPath += "/PPD_EntryDetailRecord_" + m_strDate + ".txt";

        if (File.Exists(strPath))
        {
            int index = 1;
            FileName += "/PPD_EntryDetailRecord_" + index + "_" + m_strDate + ".txt";
            while (File.Exists(FileName))
            {
                string strFilePath;
                strFilePath = Directory.GetCurrentDirectory();
                strFilePath = Directory.GetParent(strFilePath).ToString();
                strFilePath = Directory.GetParent(strFilePath).ToString();
                strFilePath = strFilePath + "\\ACH\\";
                FileName = strFilePath + "/PPD_EntryDetailRecord_" + ++index + "_" + m_strDate + ".txt";
            }
            using (TextWriter tw = new StreamWriter(FileName))
            {
                tw.Write(m_strRecordTypeCode.PadLeft(1, '0'));
                tw.Write(m_strTransactionCode.PadLeft(2, '0'));
                tw.Write(m_strRecievingDFIIdentification.PadLeft(9, '0'));
                //tw.Write(m_strCheckDigit.PadLeft(1, '0'));
                tw.Write(m_strDFIAccountNumber.PadRight(17, ' '));
                tw.Write(m_strAmount.PadLeft(10, '0'));
                tw.Write(m_strIndividualIdentificationNumber.PadRight(15, ' '));
                tw.Write(m_strIndividualName.PadRight(22, ' '));
                tw.Write(m_strDiscretionaryData.PadRight(2, ' '));
                tw.Write(m_strAddendaRecordIndicator.PadLeft(1, '0'));
                tw.Write("TTTTBBBBZZZZZZZ");
                tw.WriteLine();
                //tw.Flush();
                tw.Close();
                StreamWriter sw = File.AppendText(FileName);
                string file1 = Directory.GetCurrentDirectory();
                file1 = Directory.GetParent(file1).ToString();
                file1 = Directory.GetParent(file1).ToString();
                file1 = file1 + "\\ACH";
                string[] fileEntries = Directory.GetFiles(file1, "TempPPDAddenda.txt");
                StreamReader sr = new StreamReader(fileEntries[0]);
                string s;
                s = sr.ReadToEnd();
                sr.Close();
                sw.Write(s);
                sw.Close();
            }
        }
        if (!(File.Exists(strPath)))
        {
            using (TextWriter tw = new StreamWriter(strPath))
            {
                tw.Write(m_strRecordTypeCode.PadLeft(1, '0'));
                tw.Write(m_strTransactionCode.PadLeft(2, '0'));
                tw.Write(m_strRecievingDFIIdentification.PadLeft(9, '0'));
                tw.Write(m_strDFIAccountNumber.PadRight(17, ' '));
                tw.Write(m_strAmount.PadLeft(10, '0'));
                tw.Write(m_strIndividualIdentificationNumber.PadRight(15, ' '));
                tw.Write(m_strIndividualName.PadRight(22, ' '));
                tw.Write(m_strDiscretionaryData.PadRight(2, ' '));
                tw.Write(m_strAddendaRecordIndicator.PadLeft(1, '0'));
                tw.Write("TTTTBBBBZZZZZZZ");
                tw.WriteLine();
                tw.Close();
                StreamWriter sw = File.AppendText(strPath);
                string file1 = Directory.GetCurrentDirectory();
                file1 = Directory.GetParent(file1).ToString();
                file1 = Directory.GetParent(file1).ToString();
                file1 = file1 + "\\ACH";
                string[] fileEntries = Directory.GetFiles(file1, "TempPPDAddenda.txt");
                StreamReader sr = new StreamReader(fileEntries[0]);
                string s;
                s = sr.ReadToEnd();
                sr.Close();
                sw.Write(s);
                sw.Close();
            }
        }
        return m_flag;
    }

But at this pont i am having an issue

           strFilePath = Directory.GetCurrentDirectory();
                strFilePath = Directory.GetParent(strFilePath).ToString();
                strFilePath = Directory.GetParent(strFilePath).ToString();
                strFilePath = strFilePath + "\\ACH\\";

as per my requirement i am saving in that particular path. but when i make an exe file of this and give some one they can install directly in C: or some othe directory so inorder to overcome that problem i would like to opt the user a save file dialog so that he can save the file where he required..

Upvotes: 1

Views: 483

Answers (4)

James
James

Reputation: 82096

I think you are looking for the SaveFileDialog class. See the link for an example.

If you want to set a default path to where the user should be saving the file you can use the InitialDirectory property. If you want to set a default filename you can use the FileName property.

Example

SaveFileDialog saveFileDialog1 = new SaveFileDialog();

saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
saveFileDialog1.FilterIndex = 2;
saveFileDialog1.RestoreDirectory = true;
saveFileDialog1.InitialDirectory = Environment.CurrentDirectory;
saveFileDialog1.FileName = "MyDefaultFileName";

Upvotes: 3

SaravananArumugam
SaravananArumugam

Reputation: 3720

For such common uses we have common dialog controls in .net. These are derived from System.Windows.Forms.CommonDialog.

SaveFileDialog is the right choice for your need.

Upvotes: 0

Rox
Rox

Reputation: 2023

Try this:

string strFilePath = Path.Combine(System.Windows.Forms.Application.StartupPath, "TempPPDAddenda.txt");

Application.StartupPath: Gets the path for the executable file that started the application, not including the executable name.

Upvotes: 0

Manfred
Manfred

Reputation: 5666

Yes. That is possible. For an example in a Forms based application see MSDN.

Upvotes: 0

Related Questions