【问题标题】:Is it posible to save file with the desired name I choose是否可以使用我选择的所需名称保存文件
【发布时间】:2010-08-03 10:27:56
【问题描述】:

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

任何想法..

其实我已经写了一个代码来保存我的文件如下

        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: 或其他目录中,所以为了克服这个问题,我想为用户选择一个保存文件对话框,以便他可以将文件保存在他的位置需要..

【问题讨论】:

  • 如果尚未使用,您可以使用您选择的名称保存(您有多种处理方式,具体取决于上下文)。我不明白文件保存的哪一部分你不知道该怎么做。 (此外,发布您到目前为止的代码会有所帮助)
  • Rox 我添加了到目前为止由我完成的示例代码,我还说我的要求可以帮助我获得解决方案
  • 您的意思是将文件保存在安装应用程序的文件夹中吗?
  • 是的。但在我编写的代码中,它将修剪目录。所以它可能会导致问题。假设如果你直接在 C: 中安装了我的应用程序,那么它会引发错误,所以我想将文件保存在用户安装的应用程序中。

标签: c# desktop-application savefiledialog


【解决方案1】:

我认为您正在寻找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";

【讨论】:

  • 好的,我会检查并告诉
  • 还有一个问题,实际上我正在保存不同的文件名,如 Fileheader、BatchHeader 等。如果这些存在,我将更改我的文件名我可以默认设置文件名,如 saveFileDialog1.FileName = "FileHeader";
  • @Dorababu:是的,请参阅我正在设置默认文件名的示例。
  • @Dorababu:我更新了我的答案以使用Enviroment.CurrentDirectory,这消除了对System.Forms.Windows 参考的需要。
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    试试这个:

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

    Application.StartupPath: 获取路径 对于启动的可执行文件 申请,不包括 可执行文件名。

    【讨论】:

    • 它正在获取我的应用程序的调试路径。但我希望用户在安装我的应用程序的目录中有一个文件夹,并愿意将所有文件保存在该文件夹中
    • 您是否尝试将其安装在您计算机上的另一个驱动器/文件夹上?如果您从 Visual Studio 运行应用程序,它将返回调试路径
    • 嘿,Rox,但在类文件中我无法获取此 Path.Combine(Application.StartupPath, "") 它正在引发错误,因为找不到应用程序。
    • @Dorababu:您需要添加对System.Windows.Forms 的引用才能访问Application 类。
    • James 这个引用没有出现在我的业务层中的 .cs 文件中
    【解决方案4】:

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

    SaveFileDialog 是您需要的正确选择。

    【讨论】:

      猜你喜欢
      • 2018-04-16
      • 2021-11-17
      • 1970-01-01
      • 2011-03-31
      • 2017-04-09
      • 2012-10-19
      • 2020-07-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多