日期:2014-05-20  浏览次数:20728 次

怎么把一个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
IO

using 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();
            }
        }
    }
}