【发布时间】:2012-11-26 01:49:15
【问题描述】:
我正在尝试用 C# 开发我的第一个应用程序,我只是尝试从文件中读取和写入。我已经进行了数小时的研究,有导师试图教我,但这对我来说毫无意义。
我已经布置好表单的开头,并且我认为这是我正在使用的 StreamWriter 的正确语法,但文件名似乎在另一个进程中使用,我不知道是哪个.这是我正在使用的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace CustomerApplication
{
public partial class AddCustomerForm : Form
{
public AddCustomerForm()
{
InitializeComponent();
}
private void saveAndExitBtn_Click(object sender, EventArgs e)
{
StreamWriter sw = File.AppendText("test.csv");
sw.WriteLine("Test for Hope");
// next, retrieve hidden form's memory address
Form myParentForm = CustomerAppStart.getParentForm();
// now that we have the address use it to display the parent Form
myParentForm.Show();
// Finally close this form
this.Close();
}// end saveAndExitBtn_Click method
这是 Visual Studio 不喜欢的行:
StreamWriter sw = File.AppendText("test.csv");
提前感谢您的宝贵时间。
~TT
【问题讨论】:
-
有什么异常?
test.csv是否在可执行文件所在的目录中? -
+1 请发布例外。还有,你说的
the file name seems to be used in another process, and I have no idea which.到底是什么意思呢? -
@JamesEkema - 这可能来自异常消息,表明用户没有正确关闭流。
标签: c# csv io streamwriter