获取记事本的值并将其放入 C# 字符串中?

发布于 2024-11-13 06:00:51 字数 229 浏览 4 评论 0原文

记事本:

Hello world!

我如何将其放入 C# 中并将其转换为字符串..?

到目前为止,我已经得到了记事本的路径。

 string notepad = @"c:\oasis\B1.text"; //this must be Hello world

请建议我..我对此不熟悉..tnx

Notepad:

Hello world!

How I'll put it in C# and convert it into string..?

So far, I'm getting the path of the notepad.

 string notepad = @"c:\oasis\B1.text"; //this must be Hello world

Please advice me.. I'm not familiar on this.. tnx

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

遇到 2024-11-20 06:00:51

您可以使用 File.ReadAllText() 方法读取文本:

    public static void Main()
    {
        string path = @"c:\oasis\B1.txt";

        try {

            // Open the file to read from.
            string readText = System.IO.File.ReadAllText(path);
            Console.WriteLine(readText);

        }
        catch (System.IO.FileNotFoundException fnfe) {
            // Handle file not found.  
        }

    }

You can read text using the File.ReadAllText() method:

    public static void Main()
    {
        string path = @"c:\oasis\B1.txt";

        try {

            // Open the file to read from.
            string readText = System.IO.File.ReadAllText(path);
            Console.WriteLine(readText);

        }
        catch (System.IO.FileNotFoundException fnfe) {
            // Handle file not found.  
        }

    }
只是我以为 2024-11-20 06:00:51

您需要读取文件的内容,例如:

using (var reader = new StreamReader(new FileStream(path, FileMode.Open, FileAccess.Read))
{
    return reader.ReadToEnd();
}

或者,尽可能简单:

return File.ReadAllText(path);

You need to read the content of the file, e.g.:

using (var reader = new StreamReader(new FileStream(path, FileMode.Open, FileAccess.Read))
{
    return reader.ReadToEnd();
}

Or, as simply as possible:

return File.ReadAllText(path);
祁梦 2024-11-20 06:00:51

使用StreamReader并读取文件,如下所示

string notepad = @"c:\oasis\B1.text";
StringBuilder sb = new StringBuilder();
 using (StreamReader sr = new StreamReader(notepad)) 
            {
                while (sr.Peek() >= 0) 
                {
                    sb.Append(sr.ReadLine());
                }
            }

string s = sb.ToString();

make use of StreamReader and read the file as shown below

string notepad = @"c:\oasis\B1.text";
StringBuilder sb = new StringBuilder();
 using (StreamReader sr = new StreamReader(notepad)) 
            {
                while (sr.Peek() >= 0) 
                {
                    sb.Append(sr.ReadLine());
                }
            }

string s = sb.ToString();
∞梦里开花 2024-11-20 06:00:51

使用 File.ReadAllText

string text_in_file = File.ReadAllText(notepad);

Use File.ReadAllText

string text_in_file = File.ReadAllText(notepad);
铜锣湾横着走 2024-11-20 06:00:51

检查这个例子:

// Read the file as one string.
System.IO.StreamReader myFile =
   new System.IO.StreamReader("c:\\test.txt");
string myString = myFile.ReadToEnd();

myFile.Close();

// Display the file contents.
Console.WriteLine(myString);
// Suspend the screen.
Console.ReadLine();

check this example:

// Read the file as one string.
System.IO.StreamReader myFile =
   new System.IO.StreamReader("c:\\test.txt");
string myString = myFile.ReadToEnd();

myFile.Close();

// Display the file contents.
Console.WriteLine(myString);
// Suspend the screen.
Console.ReadLine();
卖梦商人 2024-11-20 06:00:51

从文本文件读取 (Visual C#),在此示例中,当调用 StreamReader 时,不使用 @,但是当您在 Visual Studio 中编写代码时,它将给出以下错误: <代码>\

无法识别的转义序列

要转义此错误,您可以在路径字符串开头的 " 之前写入 @
我还应该提到,如果我们使用 \\,即使我们不写 @,它也不会给出此错误。

// Read the file as one string.
System.IO.StreamReader myFile = new System.IO.StreamReader(@"c:\oasis\B1.text");
string myString = myFile.ReadToEnd();

myFile.Close();

// Display the file contents.
Console.WriteLine(myString);
// Suspend the screen.
Console.ReadLine();

Reading From a Text File (Visual C#), in this example @ is not used when StreamReader is being called, however when you write the code in Visual Studio it will give the below error for each \

Unrecognized escape sequence

To escape this error you can write @ before " that is at the beginning of your path string.
I shoul also mentioned that it does not give this error if we use \\ even if we do not write @.

// Read the file as one string.
System.IO.StreamReader myFile = new System.IO.StreamReader(@"c:\oasis\B1.text");
string myString = myFile.ReadToEnd();

myFile.Close();

// Display the file contents.
Console.WriteLine(myString);
// Suspend the screen.
Console.ReadLine();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文