System.IO.DirectoryNotFoundException异常代码:
using System;
using System.IO;
using System.Text;class Test 
{
    
public static void Main() 
{
string path = @"c:\temp\MyTest.txt"; // Delete the file if it exists.
if (File.Exists(path)) 
{
File.Delete(path);
} //Create the file.
using (FileStream fs = File.Create(path)) 
{
AddText(fs, "This is some text");
AddText(fs, "This is some more text,");
AddText(fs, "\r\nand this is on a new line");
AddText(fs, "\r\n\r\nThe following is a subset of characters:\r\n"); for (int i=1;i < 120;i++) 
{
AddText(fs, Convert.ToChar(i).ToString()); //Split the output at every 10th character.
if (Math.IEEERemainder(Convert.ToDouble(i), 10) == 0) 
{
AddText(fs, "\r\n");
}
}
} //Open the stream and read it back.
using (FileStream fs = File.OpenRead(path)) 
{
byte[] b = new byte[1024];
UTF8Encoding temp = new UTF8Encoding(true);
while (fs.Read(b,0,b.Length) > 0) 
{
Console.WriteLine(temp.GetString(b));
}
}
} private static void AddText(FileStream fs, string value) 
{
byte[] info = new UTF8Encoding(true).GetBytes(value);
fs.Write(info, 0, info.Length);
}
}
========================未处理的异常: System.IO.DirectoryNotFoundException: 未找到路径“c:\temp\MyTest.
txt”的一部分。
   at System.IO.__Error.WinIOError(Int32 errorCode, String str)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access,
FileShare share, Int32 bufferSize, Boolean useAsync, String msgPath, Boolean bFr
omProxy)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access,
FileShare share, Int32 bufferSize)
   at System.IO.File.Create(String path)
   at Test.Main() in d:\visual studio projects\file与text实例程序\consoleapplica
tion1\class1.cs:line 19

解决方案 »

  1.   

    DirectoryNotFound 未找到路径“c:\temp\MyTest.
    txt”的一部分。
    不是说的很清楚了?
      

  2.   

    using (FileStream fs = File.Create(path)) 这里不是有创建嘛??
    是第19行的问题?应该怎么解决啊?
      

  3.   

    private void button2_Click(object sender, System.EventArgs e)

    {
    StreamWriter SW;
    SW=File.CreateText("c:\\shiyan.txt");
    SW.WriteLine("123");
    SW.WriteLine("345");
    SW.Close();

    }我晕,这个代码也有同样的异常。。什么意思嘛高人在哪
      

  4.   

    如果是ntfs的分区,查看你的权限问题,或者改到其他fat32的分区测试.
      

  5.   

    我的c盘是fat32。另外这个程序好好的:public class FileClass{
    public static void Main()
    {
    WriteToFile();
    }
    static void WriteToFile()
    {
    StreamWriter SW;
    SW=File.CreateText("c:\MyTextFile.txt");
    SW.WriteLine("God is greatest of them all");
    SW.WriteLine("This is second line");
    SW.Close();
    Console.WriteLine("File Created SucacessFully");
    }
    }是不是我的.netframwork的版本问题???我崩溃了快
      

  6.   

    借个座啊 ,dlzhangln(XXXXX)谁知道怎么改括号里的名
      

  7.   

    各位,我现在做了一些实验,看到,如果是单纯写windows窗体程序,代码不会有问题;如果是在智能设备开发模块下写windows窗体程序,则文件操作会有问题,还会有一个莫名其妙的问题:MessageBox.Show(this, "保存成功!", "tips",MessageBoxButtons.OK,MessageBoxIcon.Question, MessageBoxDefaultButton.Button1,MessageBoxOptions.RightAlign);========
    找不到类型或命名空间名称“MessageBoxOptions”(是否缺少 using 指令或程序集引用?)
    这是因为智能设备模块本身的bug,还是因为我的点net有问题呢???