注册 登录
编程论坛 C# 论坛

C#编译出错,不知哪里的问题

yuma 发布于 2021-02-27 20:27, 2183 次点击
C#  读文件

程序代码:
using System;
using namespace FileApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                // 创建一个 StreamReader 的实例来读取文件
               
// using 语句也能关闭 StreamReader
                using (StreamReader sr = new StreamReader("names.txt"))
                {
                    string line;

                    // 从文件读取并显示行,直到文件的末尾
                    while ((line = sr.ReadLine()) != null)
                    {
                        Console.WriteLine(line);
                    }
                }
            }
            catch (Exception e)
            {
                // 向用户显示出错消息
                Console.WriteLine("The file could not be read:");
                Console.WriteLine(e.Message);
            }
            Console.ReadKey();
        }
    }
}
7 回复
#2
hamjam2021-03-11 19:40
出错的信息呢
#3
apull2021-03-15 23:46
namespace FileApplication
#4
y30620102472021-04-12 12:30
还可以这样写吗
试试下面这样写
程序代码:
using System;
using namespace FileApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                // 创建一个 StreamReader 的实例来读取文件
               
// using 语句也能关闭 StreamReader
                using (StreamReader sr = new StreamReader("names.txt"))
                {
                    string line;

                    // 从文件读取并显示行,直到文件的末尾
                    while ((line = sr.ReadLine()) != null)
                    {
                        Console.WriteLine(line);
                    }
                }
            }
            catch (Exception e)
            {
                // 向用户显示出错消息
                Console.WriteLine("The file could not be read:");
                Console.WriteLine(e.Message);
            }
            Console.ReadKey();
        }
    }
}
#5
y30620102472021-04-12 12:31
回复 4楼 y3062010247
奇怪,回复的代码变了
应该在using 后面回车,下一行直接写namespace FileApplication,这样就可以了,我的可以正常运行。

[此贴子已经被作者于2021-4-12 12:33编辑过]

#6
y30620102472021-04-12 12:33
发贴不能添加图片么?想添加一个截图的,找了半天没找到。
#7
apull2021-04-12 16:59
只有本站会员才能查看附件,请 登录
#8
y30620102472021-04-12 18:30
以下是引用apull在2021-4-12 16:59:15的发言:

好的,谢谢了!
1