C#怎么把textbox中的字符写到文本文档里面去?
2013-07-22
展开全部
利用IO操作,就是把textbox的值获取出来,再写入文本文档就行啦,用IO操作的时候首先要引用IO命名空间using System.IO; StreamWriter sw = File.AppendText("d:\\test1.txt"); //以追加的方式写入文件string s=textBox1.Text;//获取文件框的值
sw.WriteLine(s);//写入数据
sw.Flush();//清空缓存
sw.Close();//关闭文件
sw.WriteLine(s);//写入数据
sw.Flush();//清空缓存
sw.Close();//关闭文件
2013-07-22
展开全部
这是我写一个小小的bat文件代码. using System;
using System.Collections.Generic;
using System.Text;
using System.IO;namespace IO
{
class Program
{
static void Main(string[] args)
{
string path = @"e:\WL's IO\";
Directory.CreateDirectory(path);
//DirectoryInfo di = new DirectoryInfo(path);
//di.Create();
////Directory.CreateDirectory("e:\\hahaxixi");
//File.CreateText(@"e:\NiRiNiZiJi\" + @"abc.bat");
//File.CreateText(@"e:\NiRiNiZiJi\" + @"def.bat");
//File.CreateText(@"e:\NiRiNiZiJi\" + @"ghi.bat");
//File.CreateText(@"e:\NiRiNiZiJi\" + @"jhk.bat");
//File.CreateText(@"e:\NiRiNiZiJi\" + @"lim.bat");
////Console.WriteLine((Directory.GetFiles(@"e:\NiRiNiZiJi").Length));
//FileInfo[] files = di.GetFiles();
//// foreach(FileInfo a in files)
////{
//// Console.WriteLine(a.Name);
//}
///写入流
using (StreamWriter sw = new StreamWriter(path + "100s.bat"))
{
sw.WriteLine("shutdown -s -t 100");
} StreamWriter sw0 = File.CreateText(path + "200s.bat");
sw0.WriteLine("shutdown -s -t 200");
sw0.Dispose(); StreamWriter sw1 = File.CreateText(path + "300s.bat");
sw1.WriteLine("shutdown -s -t 300");
sw1.Dispose(); using (StreamWriter sw = File.CreateText(path + "break.bat"))
{
sw.WriteLine("shutdown -a");
} StreamWriter sw2 = new StreamWriter(path + "说明.txt");
sw2.WriteLine(@"100s.bat、200s.bat和300s.bat分别是100秒、200秒和300秒自动关机!
break.bat则是退出自动关机命令!");
sw2.Dispose();
DirectoryInfo a = new DirectoryInfo(path);
FileInfo[] files = a.GetFiles();
foreach (FileInfo file in files)
{
StreamReader sr = new StreamReader(file.FullName);
//读取完整的文件路径。
Console.WriteLine(file.Name + ": \n" + sr.ReadToEnd() + "\n");
sr.Dispose(); //FileStream fs = new FileStream(file.FullName, FileMode.OpenOrCreate);
//StreamReader sr1 = new StreamReader(fs);
////要读取的流。
//Console.WriteLine(file.Name + ": \n" + sr1.ReadToEnd() + "\n");
//sr1.Dispose();
}
}
}
}
using System.Collections.Generic;
using System.Text;
using System.IO;namespace IO
{
class Program
{
static void Main(string[] args)
{
string path = @"e:\WL's IO\";
Directory.CreateDirectory(path);
//DirectoryInfo di = new DirectoryInfo(path);
//di.Create();
////Directory.CreateDirectory("e:\\hahaxixi");
//File.CreateText(@"e:\NiRiNiZiJi\" + @"abc.bat");
//File.CreateText(@"e:\NiRiNiZiJi\" + @"def.bat");
//File.CreateText(@"e:\NiRiNiZiJi\" + @"ghi.bat");
//File.CreateText(@"e:\NiRiNiZiJi\" + @"jhk.bat");
//File.CreateText(@"e:\NiRiNiZiJi\" + @"lim.bat");
////Console.WriteLine((Directory.GetFiles(@"e:\NiRiNiZiJi").Length));
//FileInfo[] files = di.GetFiles();
//// foreach(FileInfo a in files)
////{
//// Console.WriteLine(a.Name);
//}
///写入流
using (StreamWriter sw = new StreamWriter(path + "100s.bat"))
{
sw.WriteLine("shutdown -s -t 100");
} StreamWriter sw0 = File.CreateText(path + "200s.bat");
sw0.WriteLine("shutdown -s -t 200");
sw0.Dispose(); StreamWriter sw1 = File.CreateText(path + "300s.bat");
sw1.WriteLine("shutdown -s -t 300");
sw1.Dispose(); using (StreamWriter sw = File.CreateText(path + "break.bat"))
{
sw.WriteLine("shutdown -a");
} StreamWriter sw2 = new StreamWriter(path + "说明.txt");
sw2.WriteLine(@"100s.bat、200s.bat和300s.bat分别是100秒、200秒和300秒自动关机!
break.bat则是退出自动关机命令!");
sw2.Dispose();
DirectoryInfo a = new DirectoryInfo(path);
FileInfo[] files = a.GetFiles();
foreach (FileInfo file in files)
{
StreamReader sr = new StreamReader(file.FullName);
//读取完整的文件路径。
Console.WriteLine(file.Name + ": \n" + sr.ReadToEnd() + "\n");
sr.Dispose(); //FileStream fs = new FileStream(file.FullName, FileMode.OpenOrCreate);
//StreamReader sr1 = new StreamReader(fs);
////要读取的流。
//Console.WriteLine(file.Name + ": \n" + sr1.ReadToEnd() + "\n");
//sr1.Dispose();
}
}
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-07-22
展开全部
用IO!using System.IO;StreamWrite sw = new StreamWrite(这里写上要写入的文件的路径);sw.writeline(textbox.text);//写入文件sw.close();//关闭流
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询