C# 未能找到路径“E:\html\gg.html”的一部分。

cllover 2012-05-16 10:41:40


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


using System.IO;//stream

namespace test11
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

try
{
StreamWriter outStream = new StreamWriter("E://html//gg.html", false, Encoding.GetEncoding("GB2312"));
}
catch (IOException e)
{
System.Console.WriteLine("错误:" + e);

}
}
}
}

/*
在 System.IO.DirectoryNotFoundException 中第一次偶然出现的“mscorlib.dll”类型的异常
错误:System.IO.DirectoryNotFoundException: 未能找到路径“E:\html\gg.html”的一部分。
在 System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
在 System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
在 System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
在 System.IO.StreamWriter.CreateFile(String path, Boolean append)
在 System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize)
在 System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding)
*/


前几天还好好的,昨天就出现这样的问题了,求助大神,毕业设计问题
...全文
708 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
bengkuidejianli 2013-01-18
  • 打赏
  • 举报
回复
解决了没呢???楼主把方法贴出来啊
zuowen8605 2012-05-16
  • 打赏
  • 举报
回复
我感觉应该这样吧:
if(!file.exit())
if(!directory.exit()){
directory.create();
file.create();
}
路径和文件都需要创建

[Quote=引用 10 楼 的回复:]

如果我想要的是 给定一个带有目录和子目录的文件,如何在不存在目录的时候主动创建目录然后创建文件?
怎么实现? file.create? 还是必须先Directory.Create()?
[/Quote]
叫我三三 2012-05-16
  • 打赏
  • 举报
回复
操作文件先File.Exists(path)看文件是否存在;
----------------------------------
回10L
/// <summary>
/// 创建文件夹
/// </summary>
/// <param name="folderPath">文件夹地址</param>
public static void CreatFolder(string folderPath)
{
if (!Directory.Exists(folderPath))//如果不存在就创建file文件夹
Directory.CreateDirectory(folderPath);//创建该文件夹
}
JTZP007 2012-05-16
  • 打赏
  • 举报
回复
仔细找找
cllover 2012-05-16
  • 打赏
  • 举报
回复
如果我想要的是 给定一个带有目录和子目录的文件,如何在不存在目录的时候主动创建目录然后创建文件?
怎么实现? file.create? 还是必须先Directory.Create()?
cllover 2012-05-16
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 的回复:]

引用 3 楼 的回复:
@"E:\html\gg.html
试试?

@"E:\html\gg.html “这样应该是对的,但是这文件需要存在,才可以看到结果
[/Quote]


不会再文件夹不存在的时候主动创建么
旅行着 2012-05-16
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]
@"E:\html\gg.html
试试?
[/Quote]
@"E:\html\gg.html “这样应该是对的,但是这文件需要存在,才可以看到结果
congMEIda 2012-05-16
  • 打赏
  • 举报
回复
......神奇啊!
cllover 2012-05-16
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]

试了一下代码,没问题的。估计是路径不对,有没有把html文件夹删掉啊
[/Quote]

是新建一个文件,程序中的代码在创建stream后还 outStream.Write(buffer);
如果目录不存在就创建目录,创建目录后再创建文件
cllover 2012-05-16
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

E:\\html\\gg.htm
这样呢
[/Quote]

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


using System.IO;//stream

namespace test11
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

try
{
StreamWriter outStream = new StreamWriter("E:\\html\\gg.html", false, Encoding.GetEncoding("GB2312"));
}
catch (IOException e)
{
System.Console.WriteLine("错误:" + e);

}
}
}
}

/*
在 System.IO.DirectoryNotFoundException 中第一次偶然出现的“mscorlib.dll”类型的异常
错误:System.IO.DirectoryNotFoundException: 未能找到路径“E:\html\gg.html”的一部分。
在 System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
在 System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
在 System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
在 System.IO.StreamWriter.CreateFile(String path, Boolean append)
在 System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize)
在 System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding)
*/
rybin_1987 2012-05-16
  • 打赏
  • 举报
回复
路径不对,
EdsionWang 2012-05-16
  • 打赏
  • 举报
回复
试了一下代码,没问题的。估计是路径不对,有没有把html文件夹删掉啊
zuowen8605 2012-05-16
  • 打赏
  • 举报
回复
@"E:\html\gg.html
试试?
bdmh 2012-05-16
  • 打赏
  • 举报
回复
E:\\html\\gg.htm
这样呢
EnForGrass 2012-05-16
  • 打赏
  • 举报
回复
E://html//gg.html路径对不对?
孟子E章 2012-05-16
  • 打赏
  • 举报
回复
文件夹html还存在吗

DirectoryNotFoundException

是文件夹没找到
mingcsharp 2012-05-16
  • 打赏
  • 举报
回复
("E://html//gg.html"
看到这样的路经,实在让我长了不少见识

111,126

社区成员

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

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

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