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

これを試して:

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

Application.StartupPath : アプリケーションを起動した実行可能ファイルのパスを取得します。実行可能ファイルの名前は含まれません。

于 2010-08-03T11:09:07.680 に答える
0

はい。それは可能です。Forms ベースのアプリケーションの例については、MSDNを参照してください。

于 2010-08-03T10:33:28.653 に答える
0

このような一般的な用途のために、.net には一般的なダイアログ コントロールがあります。これらは System.Windows.Forms.CommonDialog から派生しています。

SaveFileDialog は、必要に応じて適切な選択です。

于 2010-08-03T12:28:55.883 に答える