18 个解决方案
#1
什么意思?
你是要创建文件?
还是要打开一个现有的文件写入?
如果创建文件
StreamWriter sw = File.CreateText(@"c:\abc\a.text") is ok;
要是打开文件
就必须有文件在指定的路径下
你是要创建文件?
还是要打开一个现有的文件写入?
如果创建文件
StreamWriter sw = File.CreateText(@"c:\abc\a.text") is ok;
要是打开文件
就必须有文件在指定的路径下
#2
如果是打开文件是File.Open(@"c:\abc\a.text")
如果创建文件
StreamWriter sw = File.CreateText(@"c:\abc\a.text")
如果创建文件
StreamWriter sw = File.CreateText(@"c:\abc\a.text")
#3
在ASP.NET中你还要看一下是否有文件操作的权限。
#4
\-->>\\
or use @"..."
or use @"..."
#5
好想还是不行啊
#6
好像还是不行啊
#7
你有代码???帖上来看看
#8
我开始写的:
FileStream af = new FileStream(fileName,FileMode.Append);//fileName 是字符串存储的是希望保存文件的地址
StreamWriter sw = new StreamWriter(af);
sw.WriteLine("===================================================");
sw.WriteLine("姓名:{0}",this.textName.Text);
sw.WriteLine("年龄:{0}",this.textAge.Text);
sw.WriteLine("性别:{0}",(string)(this.rdoButMale.Checked?"男":"女"));
sw.WriteLine("会员:{0}",(string)(this.rdoButYes.Checked?"是":"不是"));
sw.WriteLine("===================================================");
sw.Close();
如果我希望将文件输出到我设置的路径,该怎么做,谢谢。
FileStream af = new FileStream(fileName,FileMode.Append);//fileName 是字符串存储的是希望保存文件的地址
StreamWriter sw = new StreamWriter(af);
sw.WriteLine("===================================================");
sw.WriteLine("姓名:{0}",this.textName.Text);
sw.WriteLine("年龄:{0}",this.textAge.Text);
sw.WriteLine("性别:{0}",(string)(this.rdoButMale.Checked?"男":"女"));
sw.WriteLine("会员:{0}",(string)(this.rdoButYes.Checked?"是":"不是"));
sw.WriteLine("===================================================");
sw.Close();
如果我希望将文件输出到我设置的路径,该怎么做,谢谢。
#9
你具体是想修改/追加文件还是在别的地方新建?
#10
FileMode.Append说明是要追加,当然先得有文件了。就是错在这儿了
#11
我的想法是,在没有文件的时候,能够按我设置的路径生成文件,在有了文件后添加内容的时候追加,
#12
我也试过好多种方式没成功过.
关注!
关注!
#13
FileInfo file = new ...
if(!file.Exist)
{
file.Create();
}
随手写的,可以去试试
if(!file.Exist)
{
file.Create();
}
随手写的,可以去试试
#14
希望各位兄台拔刀相助阿。
#15
string path = @"d:\aa";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
path = path+@"\MyTest.txt";
StreamWriter sw = File.AppendText(path);
sw.WriteLine("Hello");
sw.WriteLine("And");
sw.WriteLine("Welcome");
sw.Close();
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
path = path+@"\MyTest.txt";
StreamWriter sw = File.AppendText(path);
sw.WriteLine("Hello");
sw.WriteLine("And");
sw.WriteLine("Welcome");
sw.Close();
#16
>>>我的想法是,在没有文件的时候,能够按我设置的路径生成文件,在有了文件后添加内容的时候追加
try:
string fileName = @"C:\abc\def\a.txt";
int index = fileName.LastIndexOf(@"\");
if(index == - 1) return;
if(!Directory.Exists(fileName.Substring(0,index)))
Directory.CreateDirectory(fileName.Substring(0,index));
Stream stream = new FileStream(fileName,FileMode.Append);
StreamWriter writer = new StreamWriter(stream,System.Text.Encoding.Default);
writer.WriteLine("Hello");
writer.Close();
try:
string fileName = @"C:\abc\def\a.txt";
int index = fileName.LastIndexOf(@"\");
if(index == - 1) return;
if(!Directory.Exists(fileName.Substring(0,index)))
Directory.CreateDirectory(fileName.Substring(0,index));
Stream stream = new FileStream(fileName,FileMode.Append);
StreamWriter writer = new StreamWriter(stream,System.Text.Encoding.Default);
writer.WriteLine("Hello");
writer.Close();
#17
string fileName = @"C:\abc\def\a.txt";
int i=fileName.LastIndexOf("\\");
string path=fileName.Substring(0,i);
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
FileStream fs;
if(!File.Exists(fileName))
{
fs=new FileStream(fileName,FileMode.Create,FileAccess.Write,FileShare.ReadWrite);
}
else
{
fs=new FileStream(fileName,FileMode.Append,FileAccess.Write,FileShare.ReadWrite);
}
StreamWriter sw=new StreamWriter(fs,System.Text.Encoding.Default);
sw.WriteLine("===================================================");
sw.WriteLine("姓名:ddd");
sw.WriteLine("年龄:ccc");
sw.WriteLine("性别:bbb");
sw.WriteLine("会员:aaa");
sw.WriteLine("===================================================");
sw.Close();
fs.Close();
已经可以多次写入文件!
int i=fileName.LastIndexOf("\\");
string path=fileName.Substring(0,i);
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
FileStream fs;
if(!File.Exists(fileName))
{
fs=new FileStream(fileName,FileMode.Create,FileAccess.Write,FileShare.ReadWrite);
}
else
{
fs=new FileStream(fileName,FileMode.Append,FileAccess.Write,FileShare.ReadWrite);
}
StreamWriter sw=new StreamWriter(fs,System.Text.Encoding.Default);
sw.WriteLine("===================================================");
sw.WriteLine("姓名:ddd");
sw.WriteLine("年龄:ccc");
sw.WriteLine("性别:bbb");
sw.WriteLine("会员:aaa");
sw.WriteLine("===================================================");
sw.Close();
fs.Close();
已经可以多次写入文件!
#18
必须先建目录啊,不然没用的!
System.IO.Directory.CreateDirectory(path); //不用检测,存在会自动不创建
System.IO.Directory.CreateDirectory(path); //不用检测,存在会自动不创建
#1
什么意思?
你是要创建文件?
还是要打开一个现有的文件写入?
如果创建文件
StreamWriter sw = File.CreateText(@"c:\abc\a.text") is ok;
要是打开文件
就必须有文件在指定的路径下
你是要创建文件?
还是要打开一个现有的文件写入?
如果创建文件
StreamWriter sw = File.CreateText(@"c:\abc\a.text") is ok;
要是打开文件
就必须有文件在指定的路径下
#2
如果是打开文件是File.Open(@"c:\abc\a.text")
如果创建文件
StreamWriter sw = File.CreateText(@"c:\abc\a.text")
如果创建文件
StreamWriter sw = File.CreateText(@"c:\abc\a.text")
#3
在ASP.NET中你还要看一下是否有文件操作的权限。
#4
\-->>\\
or use @"..."
or use @"..."
#5
好想还是不行啊
#6
好像还是不行啊
#7
你有代码???帖上来看看
#8
我开始写的:
FileStream af = new FileStream(fileName,FileMode.Append);//fileName 是字符串存储的是希望保存文件的地址
StreamWriter sw = new StreamWriter(af);
sw.WriteLine("===================================================");
sw.WriteLine("姓名:{0}",this.textName.Text);
sw.WriteLine("年龄:{0}",this.textAge.Text);
sw.WriteLine("性别:{0}",(string)(this.rdoButMale.Checked?"男":"女"));
sw.WriteLine("会员:{0}",(string)(this.rdoButYes.Checked?"是":"不是"));
sw.WriteLine("===================================================");
sw.Close();
如果我希望将文件输出到我设置的路径,该怎么做,谢谢。
FileStream af = new FileStream(fileName,FileMode.Append);//fileName 是字符串存储的是希望保存文件的地址
StreamWriter sw = new StreamWriter(af);
sw.WriteLine("===================================================");
sw.WriteLine("姓名:{0}",this.textName.Text);
sw.WriteLine("年龄:{0}",this.textAge.Text);
sw.WriteLine("性别:{0}",(string)(this.rdoButMale.Checked?"男":"女"));
sw.WriteLine("会员:{0}",(string)(this.rdoButYes.Checked?"是":"不是"));
sw.WriteLine("===================================================");
sw.Close();
如果我希望将文件输出到我设置的路径,该怎么做,谢谢。
#9
你具体是想修改/追加文件还是在别的地方新建?
#10
FileMode.Append说明是要追加,当然先得有文件了。就是错在这儿了
#11
我的想法是,在没有文件的时候,能够按我设置的路径生成文件,在有了文件后添加内容的时候追加,
#12
我也试过好多种方式没成功过.
关注!
关注!
#13
FileInfo file = new ...
if(!file.Exist)
{
file.Create();
}
随手写的,可以去试试
if(!file.Exist)
{
file.Create();
}
随手写的,可以去试试
#14
希望各位兄台拔刀相助阿。
#15
string path = @"d:\aa";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
path = path+@"\MyTest.txt";
StreamWriter sw = File.AppendText(path);
sw.WriteLine("Hello");
sw.WriteLine("And");
sw.WriteLine("Welcome");
sw.Close();
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
path = path+@"\MyTest.txt";
StreamWriter sw = File.AppendText(path);
sw.WriteLine("Hello");
sw.WriteLine("And");
sw.WriteLine("Welcome");
sw.Close();
#16
>>>我的想法是,在没有文件的时候,能够按我设置的路径生成文件,在有了文件后添加内容的时候追加
try:
string fileName = @"C:\abc\def\a.txt";
int index = fileName.LastIndexOf(@"\");
if(index == - 1) return;
if(!Directory.Exists(fileName.Substring(0,index)))
Directory.CreateDirectory(fileName.Substring(0,index));
Stream stream = new FileStream(fileName,FileMode.Append);
StreamWriter writer = new StreamWriter(stream,System.Text.Encoding.Default);
writer.WriteLine("Hello");
writer.Close();
try:
string fileName = @"C:\abc\def\a.txt";
int index = fileName.LastIndexOf(@"\");
if(index == - 1) return;
if(!Directory.Exists(fileName.Substring(0,index)))
Directory.CreateDirectory(fileName.Substring(0,index));
Stream stream = new FileStream(fileName,FileMode.Append);
StreamWriter writer = new StreamWriter(stream,System.Text.Encoding.Default);
writer.WriteLine("Hello");
writer.Close();
#17
string fileName = @"C:\abc\def\a.txt";
int i=fileName.LastIndexOf("\\");
string path=fileName.Substring(0,i);
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
FileStream fs;
if(!File.Exists(fileName))
{
fs=new FileStream(fileName,FileMode.Create,FileAccess.Write,FileShare.ReadWrite);
}
else
{
fs=new FileStream(fileName,FileMode.Append,FileAccess.Write,FileShare.ReadWrite);
}
StreamWriter sw=new StreamWriter(fs,System.Text.Encoding.Default);
sw.WriteLine("===================================================");
sw.WriteLine("姓名:ddd");
sw.WriteLine("年龄:ccc");
sw.WriteLine("性别:bbb");
sw.WriteLine("会员:aaa");
sw.WriteLine("===================================================");
sw.Close();
fs.Close();
已经可以多次写入文件!
int i=fileName.LastIndexOf("\\");
string path=fileName.Substring(0,i);
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
FileStream fs;
if(!File.Exists(fileName))
{
fs=new FileStream(fileName,FileMode.Create,FileAccess.Write,FileShare.ReadWrite);
}
else
{
fs=new FileStream(fileName,FileMode.Append,FileAccess.Write,FileShare.ReadWrite);
}
StreamWriter sw=new StreamWriter(fs,System.Text.Encoding.Default);
sw.WriteLine("===================================================");
sw.WriteLine("姓名:ddd");
sw.WriteLine("年龄:ccc");
sw.WriteLine("性别:bbb");
sw.WriteLine("会员:aaa");
sw.WriteLine("===================================================");
sw.Close();
fs.Close();
已经可以多次写入文件!
#18
必须先建目录啊,不然没用的!
System.IO.Directory.CreateDirectory(path); //不用检测,存在会自动不创建
System.IO.Directory.CreateDirectory(path); //不用检测,存在会自动不创建