1

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..

Developer
  • 8,390
  • 41
  • 129
  • 238
  • You can save with the name you choose if it's not used already (you have several options on how to handle it, depending on the context). I don't understand what part of the file saving you don't know how to do. (Also, posting the code you have so far would help) – Rox Aug 03 '10 at 10:31
  • Rox i added my sample code that was done so far by me and also i said my requirement can u help me in getting the solution – Developer Aug 03 '10 at 10:56
  • Do you mean to offer to save the file in the folder where the application is installed? – Rox Aug 03 '10 at 11:03
  • Ya.. But in the code i written it will trim the direcotry . So it may cause problem. SUpoose if u installed my application in C: directly then it will raise an error so i would like to save the file in the application where the user installed. – Developer Aug 03 '10 at 11:06

4 Answers4

3

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";
James
  • 80,725
  • 18
  • 167
  • 237
  • Ok i will check out and will tell – Developer Aug 03 '10 at 12:17
  • One more question actually i am saving ith different file name like Fileheader, BatchHeader and so on. If those exists i will change my file name can i defaultly set the file name like saveFileDialog1.FileName = "FileHeader"; – Developer Aug 03 '10 at 12:21
  • @Dorababu: Yes see the example I am setting the default filename. – James Aug 03 '10 at 13:06
  • @Dorababu: I updated my answer to use `Enviroment.CurrentDirectory` aswell which eliminates the need for the `System.Forms.Windows` reference. – James Aug 03 '10 at 13:26
0

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

Manfred
  • 5,320
  • 3
  • 35
  • 29
0

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.

Rox
  • 1,985
  • 12
  • 19
  • It is getting the Debug path of my application.. But i would like to have the user a folder in the directory where he installed my application and willing to save all the files in that folder – Developer Aug 03 '10 at 11:17
  • Have you tried installing this on another drive/folder on your computer? It returns the debug path if you're running the app from Visual Studio – Rox Aug 03 '10 at 11:20
  • Hey Rox but in class file i am unable to get this Path.Combine(Application.StartupPath, "") It is raising an error as Application not found. – Developer Aug 03 '10 at 11:22
  • @Dorababu: You need to add a reference to the `System.Windows.Forms` to access the `Application` class. – James Aug 03 '10 at 11:30
  • James this reference is not coming in my .cs file which was in business layer – Developer Aug 03 '10 at 11:41
  • You need to add a reference to `Systems.Windows.Forms` to whichever project you are trying to access the `Application` object from. – James Aug 03 '10 at 11:46
  • @Dorababu: you can use this class in the business layer - I have added the reference in other classes, not only in the Form – Rox Aug 03 '10 at 11:46
  • Ok got it i tried to create a directory if not exists in the application as follows string currentPath = Directory.GetCurrentDirectory(); if (!Directory.Exists(Path.Combine(currentPath, "ACH"))) Directory.CreateDirectory(Path.Combine(currentPath, "ACH")); But how can i read that combied path here – Developer Aug 03 '10 at 11:52
  • Read it where? Check the path is correct while debugging, or use it later in the program? – Rox Aug 03 '10 at 11:55
  • If i use only Directory.GetCurrentDirectory or Application.StartupPath will this issue will be resolved.... – Developer Aug 03 '10 at 12:03
  • I don't understand what the issue is, but from re-reading the question I had the idea that you might want to force the user to save in the current directory, and stop them from saving on C:\ and other folders. Is that what you want to do? – Rox Aug 03 '10 at 12:07
  • @Rox: I also can't fathom out exactly what the requirement is however he did mention *"i would like to opt the user a save file dialog so that he can save the file where he required"*. So my guess is he wants to prompt the user to save the file but wants to set the default save location but still allow the user to save wherever they please. A SaveFileDialog is all he needs in that case... – James Aug 03 '10 at 12:12
  • Actually my issue is if i give my setup of my applicaton to you. You may install it in C or D or some other drives. In some systems they may save in some other logical drives. When they started running the application they will have some forms to fill. If they fill the data and click on save i would like to save the file in the directory where they installed the application .. – Developer Aug 03 '10 at 12:12
0

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.

SaravananArumugam
  • 3,680
  • 6
  • 33
  • 45