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

解决方案 »

  1.   

     用StreamWriter打开文件,writeline就行了撒
      

  2.   

    string a="a";
    FileStream fs = new FileStream("C:\a.txt",FileMode.Append);
    StreamWriter sr=new StreamWriter(fs);
    sr.WriteLine(a);
    sr.close();
    fs.close();
      

  3.   

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

  4.   

    错误 1 不能在此范围内声明名为“sr”的局部变量,因为这样会使“sr”具有不同的含义,而它已在“子级”范围中表示其他内容了 D:\work\gettxt\gettxt\Program.cs 32 26 gettxt
    比如说这个变量,是gooo1
      

  5.   

    错误 2 “System.IO.FileStream”不包含“close”的定义,并且找不到可接受类型为“System.IO.FileStream”的第一个参数的扩展方法“close”(是否缺少 using 指令或程序集引用?) D:\work\gettxt\gettxt\Program.cs 35 16 gettxt错误 1 “System.IO.StreamWriter”不包含“close”的定义,并且找不到可接受类型为“System.IO.StreamWriter”的第一个参数的扩展方法“close”(是否缺少 using 指令或程序集引用?) D:\work\gettxt\gettxt\Program.cs 34 18 gettxt
    还有两个报错呀
      

  6.   

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

  7.   

    成了,肯定给,dsdfa.ToString()这就是我的变量。
    谢谢你了。
    给我一段代码好吗?
      

  8.   

    哦,那就是FileStream不用关闭,去掉那个fs.close()呗
      

  9.   

    文件放在哪里呢?我是变量是gooo1
    我写的是控制台Program.cs。
    我是新手,各位高手,能详细点吗?
      

  10.   

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

  11.   

    更正一下最后两句
    sWriter.Write(s);
    sWriter.Close();
      

  12.   

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

  13.   


    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();
            }
        }
    }来,顶熊猫大哥一下,呵呵呵!
      

  14.   


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

  15.   

    sr.close(); 不包含“close”的定义?Close()就ok,C#区分大小写!
            string f = "e:/test.txt";
            string a = "Hello C#";
            System.IO.FileStream fs = new System.IO.FileStream(f, System.IO.FileMode.Open);
            System.IO.StreamWriter sWriter = new System.IO.StreamWriter(fs);
            sWriter.Write(a);
            sWriter.Close();
      

  16.   

    不用这么麻烦的。
    你只需要一句话就可以把你的 string 型的 _myStr 写进文件里了System.IO.File.WriteAllText("你的文件名.txt", _myStr );
      

  17.   

    为什么现在CSDN连这种低级的问题都要推荐,退步了吗?
    System.IO.File.WriteAllText("c:\\1.txt", "Hello World!", Encoding.UTF8);
    就这么简单的一句话,执行后去c盘找那个1.txt看看吧,能有什么问题?
      

  18.   

    StreamWriter  就可以啊, 这个应该很简单。
      

  19.   

    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);
            }
           
        }
    }看看这个吧
      

  20.   


    using System;
    using System.Collections.Generic;
    using System.Text;namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("写入的文件路径:");
                string path = Console.ReadLine();
                Console.WriteLine("写入的内容:");
                string content = Console.ReadLine();            if (!System.IO.File.Exists(path))
                {
                    System.IO.FileStream fs_create = System.IO.File.Create(path);
                    fs_create.Close();
                    Console.WriteLine(string.Format ("已创建文件:{0}",fs_create.Name ));
                    System.IO.File.WriteAllText(path, content, Encoding.Default);
                    Console.WriteLine("文件已经成功写入.");
                    Console.ReadLine();
                }
                using (System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Append))
                {
                    System.IO.StreamWriter sw = new System.IO.StreamWriter(fs, Encoding.Default);
                    sw.Write(content);
                    sw.Close();
                    Console.WriteLine("文件已经成功写入.");
                    Console.ReadLine();
                }
                /*
                 区别: System.IO.File.WriteAllText(path, content, Encoding.Default);会覆盖之前写入的内容,对内容一次性写入
                 *       System.IO.StreamWriter 是文件流写入的方式,可以有更多的控制,WriteAllText实际上最终还是调用StreamWriter,
                 *        StreamWriter是写文件的最终方法
                 
                 */
            }
        }
    }
      

  21.   

    我认为是你把方法参数搞错了,导致这么多错误,像我问问题一样,先去看看msdn文档就好很多了
      

  22.   

    我有疑问??StreamWriter nameStreamWriter = new StreamWriter("name.text", true);
    这种怎么判断这个流是否关闭了!???
      

  23.   

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

  24.   

    FILE *p = fopen("c:/a.txt", "wb+");
    fwrite(str.c_str(), 1, strlen(str.c_str()), p);
    fclose(p);
      

  25.   

    streamwrite程序里面选中,进入msdn就有例子。
      

  26.   

    FILE *p = fopen(@"c:/a.txt", "wb+"); 
    fwrite(str.c_str(), 1, strlen(str.c_str()), p); 
    fclose(p);
      

  27.   

    delphi读写txt文件
    ━━━━━━━━━━━━━━━━━━━━━━━━━━1. memo控件读取txt
    memo1.Lines.LoadFromFile('E:\*\*.txt');2.Procedure NewTxt(FileName:String);
    Var
    F : Textfile;
    Begin
    if fileExists(FileName) then DeleteFile(FileName); {看文件是否存在,在就刪除}
    AssignFile(F, FileName); {将文件名与变量 F 关联}
    ReWrite(F); {创建一个新的文件并命名为 ek.txt}
    Writeln(F, '将您要写入的文本写入到一个 .txt 文件');
    Closefile(F); {关闭文件 F}
    End;Procedure OpenTxt(FileName:String);
    Var
    F : Textfile;
    Begin
    AssignFile(F,FileName); {将文件名与变量 F 关联}
    Append(F); {以编辑方式打开文件 F }
    Writeln(F, '将您要写入的文本写入到一个 .txt 文件');
    Closefile(F); {关闭文件 F}
    End;Procedure ReadTxt(FileName:String);
    Var
    F : Textfile;
    str : String;
    Begin
    AssignFile(F, FileName); {将文件名与变量 F 关联}
    Reset(F); {打开并读取文件 F }
    Readln(F, str);
    ShowMessage('文件有:' +str + '行。');
    Closefile(F); {关闭文件 F}
    End;procedure TForm1.Button1Click(Sender: TObject);
    begin
    NewTxt;
    end;procedure TForm1.Button2Click(Sender: TObject);
    begin
    OpenTxt;
    end;procedure TForm1.Button3Click(Sender: TObject);
    begin
    ReadTxt;
    end;
    ------------------------------------------------------------------Procedure AppendTxt(Str:String;FileName:String);
    Var
       F:Textfile;
    Begin
    AssignFile(F, FileName);
    Append(F);
    Writeln(F, Str);
    Closefile(F);
    End;
      

  28.   

    string a="a"; 
    FileStream fs = new FileStream("C:\\a.txt",FileMode.Append); 
    StreamWriter sr=new StreamWriter(fs); 
    sr.WriteLine(a); 
    sr.close(); 
    fs.close();
      

  29.   

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

  30.   

    FileStream fs = new FileStream(@".\XXX.txt", FileMode.CreateNew, FileAccess.Write);
    StreamWriter sw = new StreamWriter(fs, Encoding.Default);
    String a="水帖";
    sw.WriteLine(a); 
    sw.close(); 
    fs.close();
      

  31.   

    把你的 string 型的 string  写进文件里了 System.IO.File.WriteAllText("你的文件名.txt", string); 
      

  32.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;namespace Read
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void btnWrite_Click(object sender, EventArgs e)
            {
                //1:写文件流
                /*方法1
                FileStream s = new FileStream(textBox1.Text, FileMode.OpenOrCreate);
                StreamWriter we = new StreamWriter(s);
                we.WriteLine(richTextBox1.Text);
                we.Close();*/            saveFileDialog1.ShowDialog();
                textBox1.Text = saveFileDialog1.FileName;
                //方法2:
                if (!string.IsNullOrEmpty(textBox1.Text))
                {
                    StreamWriter we = new StreamWriter(textBox1.Text,true);//设置是否覆盖还是追加。
                    we.WriteLine(richTextBox1.Text);
                    we.Close();            }
            }       //读文章
            private void btnRead_Click(object sender, EventArgs e)
            {
                //1:写文件流2种方法            openFileDialog1.ShowDialog();
          
                textBox1.Text = openFileDialog1.FileName;
                if (!string.IsNullOrEmpty(textBox1.Text))
                {
                   
                    StreamReader you = new StreamReader(textBox1.Text,System.Text.Encoding.Default);//防止出现编码问题。
                    //读的方法有2种(1:行读取(循环)。2:整体读取)                string ok=null;
                    while((ok=you.ReadLine())!=null)
                    {
                        richTextBox1.Text +=ok+"\n";                }
                    you.Close();               /* richTextBox1.Text = you.ReadToEnd();//整体读取*/
                }
            }        private void Form1_Load(object sender, EventArgs e)
            {        }
        }
    }
      

  33.   

    晕。这么低级的问题就能上推荐,,无语
    是不是给csdn女编排的干了
    来段vb6的
    dim fso 
    dim fs 
    set fso=createobject("scripint.filesystemobject")
    fs=fso.openastextstream("Z:\操你妈\b.txt")
    dim s as stirng
    s="我F了U"
    fs.write s
    fs.close
    set fs=nothing
    set fso=nothing