3

こんにちは、C#は非常に新しいので、お詫びします。

顧客の詳細を保存するためにテキストファイルを作成しようとしています。しかし、私はテキストファイルに名前を付けたいと思います。つまり、myfile.txtです。

確かに近いですが、surname.textを変数に変更して、作成した新しいファイルの名前にする場合は、何かが足りません。

問題のある領域の周りに2**を配置しました。

private void button1_Click(object sender, EventArgs e)
{
    try
    {
        if ((tb_firstname.Text == "") || (tb_surname.Text == "") || (tb_postcode.Text == ""))
        {
            MessageBox.Show("Missing values from textboxes!");
        }
        else if (
               ****string myfile.txt = (tb_surname.Text);
               File.Exists("myfile.txt"));****
        {
                if (MessageBox.Show("Warning: file already exists. " + 
                                    "Contents will be replaced - " + 
                                     "do you want to continue?", "File Demo",
                                     MessageBoxButtons.YesNo, MessageBoxIcon.Question,
                                     MessageBoxDefaultButton.Button1,
                                     MessageBoxOptions.DefaultDesktopOnly) == 
                    DialogResult.Yes)
                {
                    //write lines of text to file
                    StreamWriter outputStream = File.CreateText(myfile.txt);
                    outputStream.WriteLine(tb_firstname.Text);
                    outputStream.WriteLine(tb_surname.Text);
                    outputStream.WriteLine(tb_surname.Text);
                    outputStream.Close();
                    MessageBox.Show("Text written to file successfully!");
                    this.Close();
                }
                else
                {
                    MessageBox.Show("Action cancelled existing file contents not replaced!");
                    this.Close();
                }
            }
            else
            {
                //write lines of text to file
                StreamWriter outputStream = File.CreateText("myFile.txt");
                outputStream.WriteLine(tb_firstname.Text);
                outputStream.WriteLine(tb_surname.Text);
                outputStream.WriteLine(tb_postcode.Text);
                outputStream.Close();
                MessageBox.Show("Text written to file successfully!");
                this.Close();
            }
        }
        catch (Exception problem)
        {
            MessageBox.Show("Program has caused an error - " + problem.ToString());
        }
}

どんな助けでも素晴らしいでしょう!

4

2 に答える 2

2

ファイル名の一部として名前を保存しようとしていると思います。次のコードを使用して、ファイル名を生成し、それが存在するかどうかを確認できます。

最初にファイル名を生成します。

string fileName = string.Format("{0}File.txt", tb_surname.Text);

次に、変数を使用して確認します。

   File.Exists(fileName);
于 2012-11-22T21:30:49.287 に答える
2

myfile.txt毎回呼び出されるファイルを作成しています

StreamWriter outputStream = File.CreateText("myFile.txt");

これは、使用している文字列リテラルです。

あなたは次の行を持っています:

string myfile.txt = (tb_surname.Text)

これは、テキストボックスの内容を。という変数に読み込みますmyfile.txt。次に、ファイル作成コードでそれを使用する必要があります。

StreamWriter outputStream = File.CreateText(myFile.txt);

引用符がないことに注意してください。

これにより、ファイルがすでに存在する場合は上書きされます。追加する場合は、次の方法を使用する必要があります。

StreamWriter outputStream = File.AppendText(myFile.txt);
于 2012-11-22T22:27:27.870 に答える