读书人

如何把一个String 变量保存到一个.txt

发布时间: 2012-02-02 23:57:14 作者: rapoo

怎么把一个String 变量保存到一个.txt文档中去?
怎么把一个String 变量保存到一个.txt文档中去?
各位高手,请问一下怎么实现?

[解决办法]
用StreamWriter打开文件,writeline就行了撒
[解决办法]
string a="a";
FileStream fs = new FileStream("C:\a.txt",FileMode.Append);
StreamWriter sr=new StreamWriter(fs);
sr.WriteLine(a);
sr.close();
fs.close();
[解决办法]

C# code
IOusing System.IO;String csdn="回帖是一种美德!每天回帖即可获得 10 分可用分!";StreamWriter mysw=new StreamWriter(path);mysw.Write(csdn);mysw.Close();
[解决办法]
换个变量名不行吗比如sw :)
[解决办法]
private void Form1_Load(object sender, EventArgs e)
{
SaveFile("123123123",@"C:\1.txt");
}

public static void SaveFile(string p_Text, string p_Path)
{
System.IO.StreamWriter _StreamWriter = new System.IO.StreamWriter(p_Path);
_StreamWriter.Write(p_Text);
_StreamWriter.Close();
}
[解决办法]
探讨
C# codeIO

using System.IO;

String csdn="回帖是一种美德!每天回帖即可获得 10 分可用分!";
StreamWriter mysw=new StreamWriter(path);

mysw.Write(csdn);
mysw.Close();

[解决办法]
string s=dsdfa.ToString();
string f="c:\abc.txt"; /*写上你的文件路径*/
string a="a";
FileStream fs = new FileStream(f);
StreamWriter sWriter=new StreamWriter(fs);
sr.Write(s);
sr.close();

[解决办法]
更正一下最后两句
sWriter.Write(s);
sWriter.Close();
[解决办法]
探讨
用StreamWriter打开文件,writeline就行了撒

[解决办法]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string csdn = "回帖是一种美德!每天回帖即可获得 10 分可用分!";

FileStream fs = new FileStream(@"d:\a.txt",FileMode.Append );//一定不要忘记@ 啊
StreamWriter sr = new StreamWriter(fs);
sr.WriteLine(csdn);
sr.Close();
fs.Close();

}
}
}

[解决办法]
C# code
using System.IO;namespace 将string变量保存到txt文档中{    class Program    {        static void Main(string[] args)        {            string csdn="回帖是一种美德!每天回帖即可获得 10 分可用分!";            //使用Unicode编码,将csdn变量中的内容写到temp.txt文件中            StreamWriter sw = new StreamWriter("temp.txt", false, Encoding.Unicode);            sw.Write(csdn);            sw.Close();            Console.WriteLine("文件已经成功写入.");            Console.ReadLine();        }    }}
[解决办法]
C# code
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.IO;namespace ConsoleApplication1{    class Program    {        static void Main(string[] args)        {            using (StreamWriter sw = new StreamWriter(@"c:\1.txt", true))  //存在C:\1.txt里面            {                string a = "i belive i can fly";                sw.Write(a);                sw.Flush();            }        }    }} 


[解决办法]

C# code
sr.close();
[解决办法]
haha
[解决办法]
探讨
不用这么麻烦的。


你只需要一句话就可以把你的 string 型的 _myStr 写进文件里了

System.IO.File.WriteAllText("你的文件名.txt", _myStr );

[解决办法]
探讨
C# code
using System.IO;

namespace 将string变量保存到txt文档中
{
class Program
{
static void Main(string[] args)
{
string csdn="回帖是一种美德!每天回帖即可获得 10 分可用分!";
//使用Unicode编码,将csdn变量中的内容写到temp.txt文件中
StreamWriter sw = new StreamWriter("temp.txt", false, Encoding.Unicode);
sw.Writ…

[解决办法]
为什么现在CSDN连这种低级的问题都要推荐,退步了吗?

C# code
System.IO.File.WriteAllText("c:\\1.txt", "Hello World!", Encoding.UTF8);
[解决办法]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace File
{
public class Program
{
public static void Write(string filename, string content)
{
FileStream fs = new FileStream(filename, FileMode.Append);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine(content);
sw.Flush();
sw.Close();
}
static void Main(string[] args)
{
string str = "hello word";
Write(@"E:\string.txt", str);
}

}
}

看看这个吧
[解决办法]
探讨
string a="a";
FileStream fs = new FileStream("C:\a.txt",FileMode.Append);
StreamWriter sr=new StreamWriter(fs);
sr.WriteLine(a);
sr.close();
fs.close();

[解决办法]
探讨
C# codeIO

using System.IO;

String csdn="回帖是一种美德!每天回帖即可获得 10 分可用分!";
StreamWriter mysw=new StreamWriter(path);

mysw.Write(csdn);
mysw.Close();

读书人网 >C#

热点推荐