gpt4 book ai didi

c# - 将文本文件写入桌面上的文件夹

转载 作者:太空宇宙 更新时间:2023-11-03 17:12:13 24 4
gpt4 key购买 nike

我想知道如何使用 StreamWriter 将文本文件写入桌面上的特定位置,例如 C:\Users\tommy\desktop\transaction\?

    private void CreateTransaction_Click(object sender, EventArgs e)
{
StreamWriter outputFile;
outputFile = File.CreateText (TID.ToString()+".txt");

outputFile.WriteLine("Investor :" +" " + InvestorNameLabel.Text);
outputFile.WriteLine("Initial Amount" + " " +AmountLabel.Text);
outputFile.WriteLine("Date Invested" +" " +DateLabel.Text);
outputFile.WriteLine("Period Chosen" + " "+DaysInvestedLabel.Text);
outputFile.WriteLine("Rate Chosen" + " " + RateLabel.Text);
outputFile.WriteLine("Total Interest" + " " +InterestAmountLabel.Text);
outputFile.WriteLine("Transaction Number :" + " " + TransactionIDLabel.Text);
outputFile.Close();

MessageBox.Show("Transaction file for Transaction: " + TransactionIDLabel.Text + "Was Created", "Transaction File");


}

最佳答案

如果你知道路径,

       FileStream fs = new FileStream(@"C:\Users\tommy\desktop\transaction\\result.txt",                           FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
sw.BaseStream.Seek(0, SeekOrigin.End);
sw.WriteLine("Investor :" +" " + InvestorNameLabel.Text);
sw.WriteLine("Initial Amount" + " " +AmountLabel.Text);
sw.WriteLine("Date Invested" +" " +DateLabel.Text);
sw.WriteLine("Period Chosen" + " "+DaysInvestedLabel.Text);
sw.WriteLine("Rate Chosen" + " " + RateLabel.Text);
sw.WriteLine("Total Interest" + " " +InterestAmountLabel.Text);
sw.WriteLine("Transaction Number :" + " " + TransactionIDLabel.Text);
sw.Flush();
sw.Close();

否则在桌面文件夹中创建一个文件并执行上述操作

使用 Environment.SpecialFolder 获取桌面路径。

string strPath = Environment.GetFolderPath(
System.Environment.SpecialFolder.DesktopDirectory);

关于c# - 将文本文件写入桌面上的文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22205767/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com