C# 如何以字符串形式存储文件
我正在尝试以字符串形式存储此文件。但我不知道如何执行此操作??您能C# 如何以字符串形式存储文件,c#,C#,我正在尝试以字符串形式存储此文件。但我不知道如何执行此操作??您能 请帮帮我,这是我的程序 using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Diagnostics; namespace Sample_Program_For_CC_ utility { class Program { static void Main(string[] args)
请帮帮我,这是我的程序
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Diagnostics;
namespace Sample_Program_For_CC_ utility
{
class Program
{
static void Main(string[] args)
{
TextReader tr = new StreamReader(@"C://Users//Darts//Desktop//sample//00004.txt");
Console.WriteLine(tr.ReadToEnd());
tr.Close();//
}
}
}
您给出的代码到底出了什么问题还不是很清楚,但使用起来更简单。对于EAX示例:
string file = @"C:\Users\Darts\Desktop\sample\00004.txt"
string text = File.ReadAllText(file);
你可以这样做:
string text = System.IO.File.ReadAllText(@"C://Users//Darts//Desktop//sample//00004.txt");
请参见您可以按如下方式以字符串形式加载文件内容
string fileContents = tr.ReadToEnd();
String theString=tr.ReadToEnd()代码>
As.ReadToEnd()返回一个字符串。您可以使用ReadAllText
string fileContents = File.ReadAllText("c:\\filename.txt")
顺便说一句,如果你使用双反斜杠,你不需要@,你必须使用反斜杠而不是斜杠-1,这样你就不用费劲自己去弄清楚它了。你已经在一个字符串中找到了它-Console.WriteLine()@Oded我是c#的新手。这就是为什么我不知道怎么做尽管如此,您没有详细解释您遇到的问题和不理解的内容-这使您很难给出有助于理解的答案。我已使用stram reader方法阅读了该文件。现在我必须将其存储在字符串中?@user1129403:如果您正在调用ReadToEnd
,已返回字符串-只需将结果存储在变量中…此方法是否将我的文件存储在字符串中?是的,字符串fileContens现在将保存文件中的内容。不需要放置双斜杠,因为您已经将“@”逐字字符串放在转义符中:)
FileStream FS = new FileStream(@"C://Users//Darts//Desktop//sample//00004.txt", FileMode.Open, FileAccess.Read);
TextReader tr = new StreamReader(@"C://Users//Darts//Desktop//sample//00004.txt");
Console.WriteLine(tr.ReadToEnd());
tr.Close();
string fileContents = File.ReadAllText("c:\\filename.txt")