将文本文件写入桌面上的文件夹 [英] Writing Text File to Folder on Desktop
本文介绍了将文本文件写入桌面上的文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道如何使用 StreamWriter 将文本文件写入桌面上的特定位置,例如 C:\Users\tommy\desktop\transaction\?
I'm wondering how do you write a text file to a specific location on the desktop such as C:\Users\tommy\desktop\transaction\ using StreamWriter?
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();
否则在桌面文件夹中创建一个文件并执行上述操作
else create a file inside the desktop folder and do the above
使用 Environment.SpecialFolder 获取桌面路径.
Use Environment.SpecialFolder to get the path to the desktop.
string strPath = Environment.GetFolderPath(
System.Environment.SpecialFolder.DesktopDirectory);
这篇关于将文本文件写入桌面上的文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文