【发布时间】: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