1

大家好,我将使用我选择的名称保存一些文件名。就像当我在我的表单上单击保存时,我将显示一个保存对话框选项,并且在该窗口的文件名上,我希望拥有自己的文件名,例如某个或其他名称,当他单击保存时,我想保存那个文件...

任何的想法..

实际上我已经编写了一个代码来保存我的文件,如下所示

        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;
    }

但是在这个桥上我有一个问题

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

根据我的要求,我正在那个特定的路径中保存。但是当我制作一个 exe 文件并提供一些文件时,他们可以直接安装在 C: 或其他目录中,所以为了克服这个问题,我想为用户选择一个保存文件对话框,以便他可以将文件保存在他的位置必需的..

4

4 回答 4

3

我认为您正在寻找SaveFileDialog类。有关示例,请参见链接。

如果要设置用户保存文件的默认路径,可以使用InitialDirectory属性。如果要设置默认文件名,可以使用FileName属性。

例子

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";
于 2010-08-03T10:30:22.043 回答
0

是的。这是可能的。有关基于表单的应用程序中的示例,请参阅MSDN

于 2010-08-03T10:33:28.653 回答
0

试试这个:

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

Application.StartupPath:获取启动应用程序的可执行文件的路径,不包括可执行文件名。

于 2010-08-03T11:09:07.680 回答
0

对于此类常见用途,我们在 .net 中有常见的对话框控件。这些是从 System.Windows.Forms.CommonDialog 派生的。

SaveFileDialog 是您需要的正确选择。

于 2010-08-03T12:28:55.883 回答