代码之家  ›  专栏  ›  技术社区  ›  Developer

是否可以用我选择的所需名称保存文件?

  •  1
  • Developer  · 技术社区  · 14 年前

    嗨,我将用我选择的名称保存一些文件名。例如,当我单击窗体上的“保存”时,我将显示一个“保存”对话框选项,在该窗口的文件名上,我希望使用自己的文件名,如某些名称或其他名称,当他单击“保存”时,我希望保存该文件…

    任何想法…

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

            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 回复  |  直到 14 年前
        1
  •  3
  •   James    14 年前

    我想你在找 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";
    
        2
  •  0
  •   Manfred    14 年前

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

        3
  •  0
  •   Rox    14 年前

    试试这个:

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

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

        4
  •  0
  •   SaravananArumugam    14 年前

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

    savefiledialog是您需要的正确选择。