我怎么写“真”在C#中的文件流 [英] How I Can Write "True" In Filestream In C#

查看:85
本文介绍了我怎么写“真”在C#中的文件流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用此代码时总是在我的txt文件中写truee请帮助我



when i use this code always write "truee" in my txt file please help me

private void checkBox1_CheckedChanged(object sender, EventArgs e)
       {
           if (checkBox1.Checked == true)
           {
               FileStream a = new FileStream("D:\\salam.txt", FileMode.OpenOrCreate, FileAccess.Write);
               StreamWriter b = new StreamWriter(a);
               b.Write(true);
               b.Close();
           }
           else
           {
               FileStream d = new FileStream("D:\\salam.txt", FileMode.OpenOrCreate, FileAccess.Write);
               StreamWriter c = new StreamWriter(d);

               c.Write(false);
               c.Close();
           }
       }

推荐答案

这并不重要。流本身支持任何数据,二进制或文本,并完全从这些数据的语义中抽象出来。以同样的方式读取和写入同一个对象非常重要。比如,你把Booleans写成True或False,重要的是你将这些字符串解释为 bool true false ,同样的事情是你使用二进制或文本0或1(或0和1)。



也许你只是需要存储和恢复一些数据对象。如果是这样,您需要学习序列化,尤其是数据合同。然后,您不需要直接使用文件(甚至数据格式)。请参阅:

http://en.wikipedia.org/wiki/Serialization [< a href =http://en.wikipedia.org/wiki/Serializationtarget =_ blanktitle =New Window> ^ ],

http://msdn.microsoft.com/en-us/library/7ay27kt9%28v=vs .110%29.aspx [ ^ ],

http:/ /msdn.microsoft.com/en-us/library/ms233843.aspx [ ^ ],

http ://msdn.microsoft.com/en-us/library/ms733127.aspx [ ^ ]。



另见我过去的答案:

如何在c#中创建常用方法并通过公共类名? [ ^ ] ,

如何我可以在表单应用程序中使用XML文件写入器和阅读器吗? [ ^ ],

创建属性文件...... [ ^ ],

解析json字符串数组 [ ^ ]。



-SA
It doesn't really matter. Streams themselves support any data, binary or text and are totally abstracted from the semantic of this data. It's important that you read and write the same object in the same way. Say, it you write Booleans as "True" or "False", it's important that you interpret these strings as bool true or false, same thing is you are using, say, binary or text 0 or 1 (or "0" and "1").

Perhaps you just need to store and restore some data objects. If so, you need to learn serialization, especially Data Contract. Then you would not need to work with files (or even data formats) directly. Please see:
http://en.wikipedia.org/wiki/Serialization[^],
http://msdn.microsoft.com/en-us/library/7ay27kt9%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/ms233843.aspx[^],
http://msdn.microsoft.com/en-us/library/ms733127.aspx[^].

See also my past answers:
How to create common method in c# and pass common class name ?[^],
How can I utilize XML File streamwriter and reader in my form application?[^],
Creating property files...[^],
deseralize a json string array[^].

—SA


引用:

当我使用此代码总是在我的txt文件中写truee

when i use this code always write "truee" in my txt file



它只有一半是真的

当你打开现有文件时,你开始从0开始写位置。所以,如果你有False在哪里,你写为True,第二个'e'从False开始

试试这个附加值


it is only half true
when you open existing file, you begin to write from the 0 position. So, if you had False where, you write True and the 2nd 'e' is left from False
try this to append values

private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            FileStream a = new FileStream("D:\\salam.txt", FileMode.OpenOrCreate, FileAccess.Write);
            StreamWriter b = new StreamWriter(a);
            a.Position = a.Length;
            b.Write(checkBox1.Checked);
            b.Close();
        }



或使用FileMode.Create写入空文件


or use FileMode.Create to write in empty file


尝试下面应该有效的代码。编写已检查和未检查事件的代码。



private void checkBox1_CheckedChanged(object sender,EventArgs e)

{

WriteStatus(checkBox1.Checked);

}



private void checkBox1_UnCheckedChanged(object sender,EventArgs e)

{

WriteStatus(checkBox1.Checked);

}



private void WriteStatus(bool?checked )

{

if(checked == true)

{

FileStream a = new FileStream(D :\\salam.txt,FileMode.OpenOrCreate,FileAccess.Write);

StreamWriter b = new StreamWriter(a);

b.Write(true) ;

b.Close();

}

else

{

FileStream d = new FileStream(D:\\salam.txt,FileMode.OpenOrCreate,FileAccess.Write);

StreamWriter c = new StreamWriter(d);



c.Write(false);

c.Close();

}

}
Try below code it should work. Write code for both checked and unchecked events.

private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
WriteStatus(checkBox1.Checked);
}

private void checkBox1_UnCheckedChanged(object sender, EventArgs e)
{
WriteStatus(checkBox1.Checked);
}

private void WriteStatus(bool? checked)
{
if (checked == true)
{
FileStream a = new FileStream("D:\\salam.txt", FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter b = new StreamWriter(a);
b.Write(true);
b.Close();
}
else
{
FileStream d = new FileStream("D:\\salam.txt", FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter c = new StreamWriter(d);

c.Write(false);
c.Close();
}
}


这篇关于我怎么写“真”在C#中的文件流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆