【问题标题】:Read a file into a string in C#在 C# 中将文件读入字符串
【发布时间】:2016-10-25 16:11:54
【问题描述】:

我正在尝试将文件读入字符串,然后将其发送到另一个应用程序。

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FileApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                // Create an instance of StreamReader to read from a file.
                // The using statement also closes the StreamReader.
                using (StreamReader sr = new StreamReader("c:\text.txt"))
                {
                    string line;

                    // Read and display lines from the file until 
                    // the end of the file is reached. 
                    while ((line = sr.ReadLine()) != null)
                    {
                        Console.WriteLine(line);
                    }
                }
            }
            catch (Exception e)
            {

                // Let the user know what went wrong.
                Console.WriteLine("The file could not be read:");
                Console.WriteLine(e.Message);
            }

            Console.ReadKey();
        }
    }
}

我收到错误:

The file could not be read:
Could not find file 'c:\users\andymarkmn\documents\visual studio 2015\Projects\FileApplication\FileApplication\bin\Debug\text.txt'.

我也尝试将文件放入 bin 调试文件夹中。

如何确保代码有效?

编辑:按照建议,我尝试使用不同的方法。

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FileApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            string filepath = "c:\\text.txt";

            try
            {
                string lines = File.ReadAllText(filepath);
                Console.Write(lines);
            }
            catch (Exception e)
            {

                // Let the user know what went wrong.
                Console.WriteLine("The file could not be read:");
                Console.WriteLine(e.Message);
            }

            Console.ReadKey();
        }
    }
}

我仍然收到错误:

The file could not be read:
Could not find file 'c:\text.txt'.

【问题讨论】:

  • 无助于解决问题,但File.ReadAllLines 会更容易
  • 错误消息的文件名与您尝试阅读的文件名不同。
  • @stuartd 嗨,你能不能也给一个代码示例。我是 c# 和 .net 堆栈的新手。
  • 你有这个文件吗c:\text.txt
  • string[] lines = File.ReadAllLines(filePath) - docs,或者File.ReadAllText,如果你只想将文本作为一个大字符串

标签: c# visual-studio file error-handling console


【解决方案1】:

您不小心在文件名字符串中使用了不需要的转义序列。

new StreamReader("c:\text.txt")

应该是

new StreamReader(@"c:\text.txt")

否则\ 被视为转义字符,\t 是制表符。这会导致意外结果和错误的文件路径。

@ 指示编译器忽略字符串中的任何转义字符。

【讨论】:

  • 我尝试使用 "c:\\text.txt" 。我得到同样的错误。
  • 您确定您正在运行该版本吗?在 filePath 变量上添加一个 Console.WriteLine,这样您就可以确保它已获取您的更改。
  • Console.WriteLine(filepath) 给出 c:\text.txt 这不正确吧?
  • 是的,没错。最后的字符串应该看起来像c:\test.txt 您是否以管理员身份运行您的视觉工作室/应用程序?也许它剥夺了你对 C 根的权利。
【解决方案2】:

"c:\text.txt" 将不起作用,因为\ 是转义字符。

使用@"c:\text.txt""c:\\text.txt"

【讨论】:

  • 错误提示文件不存在(或者你没有权限知道它的存在)
【解决方案3】:

StreamReader 被赋予一个非限定路径作为参数时,它会像你所做的那样在应用程序的工作目录中查找该文件:

using (StreamReader sr = new StreamReader("c:\text.txt"))

如果文件不在那里,可能你应该给StreamReader一个完全限定的路径:

using (StreamReader sr = new StreamReader(@"c:\text.txt"))
//or...
using (StreamReader sr = new StreamReader("c:\\text.txt"))

【讨论】:

  • 或使用管理员权限运行您的 Visual Studio...也许可以。
  • 我把它放在d里,还是一样的错误。文件路径变量只读取一个\?
  • @AndyMarkman 然后尝试使用 Admin Rights 运行应用程序...进一步检查filepathfilename中是否有任何拼写错误
【解决方案4】:

试试:

using (StreamReader sr = new StreamReader(@"C:\text.txt"))

如果使用c:\text,C# 认为字符串在C:text.txt 之间有一个制表符。

【讨论】:

    猜你喜欢
    • 2011-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-13
    • 1970-01-01
    • 2011-08-01
    • 1970-01-01
    相关资源
    最近更新 更多