【发布时间】:2016-09-19 14:18:50
【问题描述】:
以下代码是用于调用.txt-file 并将其转换为字符串的代码。
string[] lines = System.IO.File.ReadAllLines(@"C:\This PC\Documents\Visual Studio 2015\Projects\test_read_txt\bin\Debug\read.txt");
为什么 C# 不能识别 .txt-file 的位置?
我尝试使用StreamReader,但无法修改读取。
class Program
{
static void Main(string[] args)
{
// Define our one and only variable
string[] M = new string[13];
// Read the text from the text file, and insert it into the array
StreamReader SR = new StreamReader(@"read.txt");
for (int i = 0; i < 13; i++)
{
M[i] = SR.ReadLine();
}
// Close the text file, so other applications/processes can use it
SR.Close();
// Write the array to the Console
Console.WriteLine("The array from the txt.file: ");
for (int i = 0; i < 13; i++)
{
Console.WriteLine(M[i]); // Displays the line to the user
}
// Pause the application so the user can read the information on the screen
Console.ReadLine();
if (2 == 2)
{
M[1][1] = 1;
}
}
}
它没有将M[1][1] 识别为第二个数组的第二个元素,因为它只读取文件并没有将其转换为多维数组。以下代码可以用于上述问题吗?
tring[] lines = System.IO.File.ReadAllLines(@"C:\This PC\Documents\Visual Studio 2015\Projects\test_read_txt\bin\Debug\read.txt");
【问题讨论】:
-
您的路径中似乎缺少
test_read_txt。从屏幕截图中可以看出,路径中有两个具有该名称的文件夹,而您只包括一个。
标签: c# directory streamreader file.readalllines