zoukankan      html  css  js  c++  java
  • C# 如何读取文本文件中的内容

    Example

    class ReadFromFile
    {
        static void Main()
        {
            // The files used in this example are created in the topic
            // How to: Write to a Text File. You can change the path and
            // file name to substitute text files of your own.
    
            // Example #1
            // Read the file as one string.
            string text = System.IO.File.ReadAllText(@"C:UsersPublicTestFolderWriteText.txt");
    
            // Display the file contents to the console. Variable text is a string.
            System.Console.WriteLine("Contents of WriteText.txt = {0}", text);
    
            // Example #2
            // Read each line of the file into a string array. Each element
            // of the array is one line of the file.
            string[] lines = System.IO.File.ReadAllLines(@"C:UsersPublicTestFolderWriteLines2.txt");
    
            // Display the file contents by using a foreach loop.
            System.Console.WriteLine("Contents of WriteLines2.txt = ");
            foreach (string line in lines)
            {
                // Use a tab to indent each line of the file.
                Console.WriteLine("	" + line);
            }
    
            // Keep the console window open in debug mode.
            Console.WriteLine("Press any key to exit.");
            System.Console.ReadKey();
        }
    }
    

    How to read resource text file

    text file -> properties -> build function -> Embedded resource
    build project

            public string readFile(string fileName)
            {
                var assembly = Assembly.GetExecutingAssembly();
                string resourceString;
                string resourcePath = assembly.GetManifestResourceNames()
                    .Single(str => str.EndsWith(fileName));
                using (Stream stream = assembly.GetManifestResourceStream(resourcePath))
                using (StreamReader reader = new StreamReader(stream))
                {
                    return resourceString = reader.ReadToEnd();
                }
            }
    
  • 相关阅读:
    .a包生成64位
    iOS教程
    iOS 难题解决日志------2层控制器 上面的控制器显示透明
    企业级的App发布流程
    如何从oc中去获取一个私有的变量.....
    iOS app的破解原理,就是去除已付费的账户信息的原理是什么?
    Could not launch "app_name"
    GCD时间轴
    Win8自动更新怎么关闭 取消Win8自动更新
    python3 elf文件解析
  • 原文地址:https://www.cnblogs.com/francisforeverhappy/p/CsharpReadFile.html
Copyright © 2011-2022 走看看