文件输出路径问题

powerpanda 2003-12-11 11:53:22
我想将一些内容输出到文件上,在设定文件的路径时,假定设置的是:c:\a.txt就能找到路径,如果设置为:c:\abc\a.txt 系统就会说找不到路径,请问这是怎么一回事,如果我想任意的设置路径我该怎么做,比如说设置成c:\abc\a.txt ,谢谢。
...全文
86 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
FileNewExit 2003-12-12
  • 打赏
  • 举报
回复
>>>我的想法是,在没有文件的时候,能够按我设置的路径生成文件,在有了文件后添加内容的时候追加


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();
rock1981 2003-12-12
  • 打赏
  • 举报
回复
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();
Programmersheaven 2003-12-12
  • 打赏
  • 举报
回复
必须先建目录啊,不然没用的!

System.IO.Directory.CreateDirectory(path); //不用检测,存在会自动不创建
dldl 2003-12-12
  • 打赏
  • 举报
回复
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();
已经可以多次写入文件!
acewang 2003-12-11
  • 打赏
  • 举报
回复
你具体是想修改/追加文件还是在别的地方新建?
powerpanda 2003-12-11
  • 打赏
  • 举报
回复
我开始写的:
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();
如果我希望将文件输出到我设置的路径,该怎么做,谢谢。
NicholasZhr 2003-12-11
  • 打赏
  • 举报
回复
你有代码???帖上来看看
powerpanda 2003-12-11
  • 打赏
  • 举报
回复
好像还是不行啊
powerpanda 2003-12-11
  • 打赏
  • 举报
回复
好想还是不行啊
acewang 2003-12-11
  • 打赏
  • 举报
回复
\-->>\\
or use @"..."
Jinniu 2003-12-11
  • 打赏
  • 举报
回复
在ASP.NET中你还要看一下是否有文件操作的权限。
ETstudio 2003-12-11
  • 打赏
  • 举报
回复
如果是打开文件是File.Open(@"c:\abc\a.text")
如果创建文件
StreamWriter sw = File.CreateText(@"c:\abc\a.text")
brightheroes 2003-12-11
  • 打赏
  • 举报
回复
什么意思?
你是要创建文件?
还是要打开一个现有的文件写入?

如果创建文件

StreamWriter sw = File.CreateText(@"c:\abc\a.text") is ok;
要是打开文件

就必须有文件在指定的路径下

powerpanda 2003-12-11
  • 打赏
  • 举报
回复
希望各位兄台拔刀相助阿。
速马 2003-12-11
  • 打赏
  • 举报
回复
FileInfo file = new ...
if(!file.Exist)
{
file.Create();
}

随手写的,可以去试试
rock1981 2003-12-11
  • 打赏
  • 举报
回复
我也试过好多种方式没成功过.
关注!
powerpanda 2003-12-11
  • 打赏
  • 举报
回复
我的想法是,在没有文件的时候,能够按我设置的路径生成文件,在有了文件后添加内容的时候追加,
qiaoba 2003-12-11
  • 打赏
  • 举报
回复
FileMode.Append说明是要追加,当然先得有文件了。就是错在这儿了

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧