FileMode.CreateNew或File.Create没有创建任何内容而且没有错误

时间:2015-01-22 11:07:02

标签: c# .net file-io io

我有这个简单的基本代码来创建新文件并向其写入一些数据。 问题是当我运行程序时,我没有得到任何东西,既没有创建文件也没有显示任何错误消息或异常,即使在调试代码时也没有出现错误。

这是代码:

namespace WriteToFile
    {
        class WriteData
        {
            static void Main(string[] args)
            {
                string nFile = @"C:\Users\Ashraf\Documents\newFilex.txt";
                FileStream fs = null;

                // checking if file already exists and getting user input on deleting it or not
                if (File.Exists(nFile))
                {
                    Console.WriteLine("File already exists!");
                    Console.WriteLine("What would you like to do? \n");
                    Console.WriteLine("\"DELETE\" to delete the file, or press Enter to continue ?");
                    string ans;
                    ans = Console.ReadLine();
                    switch (ans)
                    {
                        case "":
                            Console.WriteLine("continue ...");
                            break;
                        case "DELETE":
                            File.Delete(nFile);
                            if (File.Exists(nFile))
                            {
                                Console.WriteLine("Error deleting file!");
                            }
                            else
                            {
                                Console.WriteLine("File deleted successfuly");
                            }
                            break;
                        default:
                            break;
                    }


                    // continue creating new file and inserting data to it
                    try
                    {
                        fs = new FileStream(nFile, FileMode.CreateNew, FileAccess.ReadWrite);
                        for (char c = 'A'; c <= 'Z'; c++)
                        {
                            fs.WriteByte((byte)c);
                        }
                        Console.WriteLine("File created and data written to file successfuly");
                    }
                    catch (IOException ce)
                    {
                        Console.WriteLine("\t File couldn't be created! \t \n" + ce.Message);
                    }
                    finally
                    {
                        Console.Read();
                        fs.Dispose();
                    }
                }
            }
        }
    }

2 个答案:

答案 0 :(得分:2)

你的缩进错了:试着这个,if(exists)try { Create在同一级别上:

if (File.Exists(nFile))
{
    Console.WriteLine("File already exists!");
    ...
}
// continue creating new file and inserting data to it
try
{
    fs = new FileStream(nFile, FileMode.CreateNew, FileAccess.ReadWrite);
    ...
}

答案 1 :(得分:1)

在程序的逻辑流程中发现问题,因为if语句阻止了其余代码的执行。 正确的代码:

class WriteData
    {
        static void Main(string[] args)
        {
            string nFile = @"C:\Users\Ashraf\Documents\newFilex.txt";
            FileStream fs = null;

            // checking if file already exists and getting user input on deleting it or not
            if (File.Exists(nFile))
            {
                Console.WriteLine("File already exists!");
                Console.WriteLine("What would you like to do? \n");
                Console.WriteLine("\"DELETE\" to delete the file, or press Enter to continue ?");
                string ans;
                ans = Console.ReadLine();
                switch (ans)
                {
                    case "":
                        Console.WriteLine("continue ...");
                        break;
                    case "DELETE":
                        File.Delete(nFile);
                        if (File.Exists(nFile))
                        {
                            Console.WriteLine("Error deleting file!");
                        }
                        else
                        {
                            Console.WriteLine("File deleted successfuly");
                        }
                        break;
                    default:
                        break;
                }
            }
            else
            {
                try
                {
                    fs = new FileStream(nFile, FileMode.CreateNew, FileAccess.ReadWrite);

                    // Write Abc to file
                    for (char c = 'A'; c <= 'Z'; c++)
                    {
                        fs.WriteByte((byte)c);
                    }

                    Console.WriteLine("File created and data written to file successfuly");
                }

                catch (IOException ce)
                {
                    Console.WriteLine("\t File couldn't be created! \t \n" + ce.Message);
                }
                finally
                {
                    Console.Read();
                    fs.Dispose();
                }
            }
        }
    }