views:

88

answers:

5

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

+3  A: 

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
Ok i will check out and will tell
Dorababu
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 likesaveFileDialog1.FileName = "FileHeader";
Dorababu
@Dorababu: Yes see the example I am setting the default filename.
James
@Dorababu: I updated my answer to use `Enviroment.CurrentDirectory` aswell which eliminates the need for the `System.Forms.Windows` reference.
James
A: 

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

John
A: 

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
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
Dorababu
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
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.
Dorababu
@Dorababu: You need to add a reference to the `System.Windows.Forms` to access the `Application` class.
James
James this reference is not coming in my .cs file which was in business layer
Dorababu
You need to add a reference to `Systems.Windows.Forms` to whichever project you are trying to access the `Application` object from.
James
@Dorababu: you can use this class in the business layer - I have added the reference in other classes, not only in the Form
Rox
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
Dorababu
Read it where? Check the path is correct while debugging, or use it later in the program?
Rox
If i use only Directory.GetCurrentDirectory or Application.StartupPath will this issue will be resolved....
Dorababu
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
@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
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 ..
Dorababu
See James' updated answer, I think it answers your question.
Rox
A: 

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
A: 

Hi James and Rox thanks for your help i have written the foolowing code to save my file with the my own name

        Stream myStream;
            string m_strDate = DateTime.Now.ToShortDateString();
            m_strDate = m_strDate.Replace("/", ".");

            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.Filter = "(*.txt)|*.txt";
            saveFileDialog1.FilterIndex = 2;
            saveFileDialog1.RestoreDirectory = true;
            saveFileDialog1.InitialDirectory = System.Windows.Forms.Application.StartupPath;
            string strFileName=saveFileDialog1.FileName = "Fileheader" + m_strDate;

            string strPath = saveFileDialog1.InitialDirectory + "/" + strFileName + ".txt";
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                if ((myStream = saveFileDialog1.OpenFile()) != null)
                {
                    StringComparison compareType = StringComparison.InvariantCultureIgnoreCase;
                    //string extension = Path.GetExtension(saveFileDialog1.FileName);
                    if (strFileName.StartsWith("FileHeader", compareType))
                    {
                        objBL.SaveFileHeader(strPath);
                        MessageBox.Show("Inserted Successfully");
                        this.Hide();
                        myStream.Close();
                    }
                    else
                    {
                        MessageBox.Show("Invalid FileName");
                    }

                }
            }

But the else case is not working here if i edit some name it is not going to else part cany you people help me

Dorababu