using (StreamReader sr = new StreamReader (Convert.ToString(adsClient.ReadAny(hActVel, typeof(double)))))
using (StreamWriter sw = new StreamWriter(NameYourFile.Text + DateTime.Today.ToString("MM_dd_yyyy") + ".csv"))
{
if (sr == null)
return;
string line;
while ((line = sr.ReadLine()) != null)
{
string[] columns = line.Split(',');
sw.WriteLine(hActVel);
}
}
我还想要一个如何编写文本框数据(即双倍)的答案,它每秒都会改变为(.c s v)文件?在c#windows form ..
我在我的项目中使用此应用程序来创建伺服电机的前端。
我已经在
中尝试了这个问题http://area51.stackexchange.com
但是我得到任何答案....请帮助我..
答案 0 :(得分:3)
创建名称为文本框的文件:
string path = @"D:\"+NameYourFile.Text+".csv";
File.Create(path).Dispose();
或者你想写文字:
string text = textbox.text; // your textbox
// WriteAllText creates a file, writes the specified string to the file,
// and then closes the file.
System.IO.File.WriteAllText(path, text);
将文本框写入CSV文件:
//string filePath = @"C:\test.csv";
string delimiter = ",";
string outputtofile = textbox.text;
int length = outputfile.length;
StringBuilder sb = new StringBuilder();
for (int index = 0; index < length; index++)
sb.AppendLine(string.Join(delimiter, output[index]));
File.WriteAllText(path, sb.ToString());
如果你在文本框Abcdefg中有这样的文字 然后它会像文件一样正确: A,b,c,d,e,f,g
答案 1 :(得分:1)
假设您有2个TextBoxes ValueTextBox和FileNameTextBox。如果ValueTextBox中的值自动更改,您可以注册TextChaned事件并将文本附加到文件,如下所示:
private void ValueTextBox_TextChanged(object sender, EventArgs e)
{
StreamWriter writer = File.AppendText(FileNameTextBox.Text);
writer.WriteLine(ValueTextBox.Text);
writer.Close();
}
或者你可以使用一个Timer控件,它会每秒触发一次,从ValueTextBox读取值并将其附加到文件中。
private void timer1_Tick(object sender, EventArgs e)
{
StreamWriter writer = File.AppendText(FileNameTextBox.Text);
writer.WriteLine(String.Format("{0},{1},{2}", DateTime.Now.ToString("hh:mm:ss"), BigMotorTextBox.Text, SmallMotorTextBox.Text));
writer.Close();
}
根据您的上一条评论:
在表单加载中,创建文件并将标题写入其中,如下所示:
private void Form2_Load(object sender, EventArgs e)
{
StreamWriter writer = new StreamWriter("Path_Of_The_File", false);
writer.WriteLine(String.Format("{0},{1},{2}", "\"Time\"", "\"Big motor Actual Velocity\"", "\"Small Motor Actual Velocity\""));
writer.close();
}
在Timer tick事件中,按如下所示写 值:
private void timer1_Tick(object sender, EventArgs e)
{
StreamWriter writer = File.AppendText(FileNameTextBox.Text);
writer.WriteLine(String.Format("{0},{1},{2}", DateTime.Now.ToString("hh:mm:ss"), BigMotorTextBox.Text, SmallMotorTextBox.Text));
writer.Close();
}
答案 2 :(得分:1)
最后我的代码变得像这样......
private void WriteToCSV_Click(object sender, EventArgs e)
{
string path = NameYourFile.Text + "_" + DateTime.Now.ToString("MM_dd_yyyy") + ".csv";
if (!System.IO.File.Exists(path))
{
// Create file if no Exists
using (StreamWriter sw = File.CreateText(path))
{
sw.WriteLine(string.Format("{0},{1},{2}", "\"Time\"", "\"Big Motor Actual Velocity\"", "\"Small Motor Actual Velocity\""));
}
}
this.timer2.Start();
}
private void CreateCSVFile_Click(object sender, EventArgs e)
{
this.timer2.Stop();
MessageBox.Show("File is Created");
}
private void timer2_Tick(object sender, EventArgs e)
{
string dt = DateTime.Now.ToString("hh.mm.ss.ffffff");
string var1 = (dt);
string var2 = adsClient.ReadAny(hActVel, typeof(double)).ToString(); ;
string var3 = adsClient.ReadAny(hSActVel, typeof(double)).ToString(); ;
StreamWriter sw = File.AppendText(NameYourFile.Text + "_" + DateTime.Today.ToString("MM_dd_yyyy") + ".csv");
{
if (BigMotorActualVelocity.Checked && SmallMotorActualVelocity.Checked)
{
sw.WriteLine(string.Format("{0},{1},{2}",var1, var2, var3));
}
else if (BigMotorActualVelocity.Checked)
{
sw.WriteLine(string.Format("{0},{1}",var1 , var2));
}
else if (SmallMotorActualVelocity.Checked)
{
sw.WriteLine(string.Format("{0},{1},{2}",var1," ",var3));
}
else
{
sw.WriteLine(string.Format("{0}",var1));
}
sw.Dispose();
}