繁体   English   中英

从 C# 中的文本文件读取自定义文本数据

[英]Read custom text data from text file in c#

我在文本文件中有以下数据。 我需要读取每个应用程序名称的服务实例名称、机器名称及其对应的状态。 我正在用C#编写asp.net控制台应用程序。 任何帮助表示赞赏。 我真的很震惊。

----- data.txt 文件数据 ---- 应用名称:推送服务名称:PushEvent.par 部署状态:成功服务实例名称:PushEve_PA 机器名称:machine1 状态:运行服务实例名称:PushEve_PA-1 机器名称: machine2 状态:备用应用程序名称:pull 服务名称:Invoke.par 部署状态:禁用服务实例名称:Invo_PA 部署状态:禁用机器名称:machine3 状态:未知服务实例名称:Invo_PA-1 部署状态:禁用机器名称:machine4 状态: 未知

尝试以下方法:

    static void Main(string[] args)
    {
        Console.WriteLine("Application Name  Service Instance Name  Machine Name         Status");

        string data = File.ReadAllText(@"Path\Data.txt");
        var appArray = data.Split("Application ",StringSplitOptions.RemoveEmptyEntries);

        foreach (var app in appArray)
        {
            var appName = PullValue(app,"Name");
            var serviceArray = app.Split("Service Instance ", StringSplitOptions.RemoveEmptyEntries);
            var services = serviceArray.Skip(1).ToArray();
            foreach(var item in services)
            {
                var serviceInstanceName = PullValue(item, "Name");
                var machineName = PullValue(item, "Machine Name");
                var status = PullValue(item, "Status");
                Console.WriteLine("    {0}            {1}            {2}            {3}",appName, serviceInstanceName, machineName, status);

            }
        }
    }
    private static string PullValue(string line, string key)
    {
        key = key + ": ";
        int ndx = line.IndexOf(key, 0, StringComparison.InvariantCultureIgnoreCase);
        if (ndx >= 0)
        {
            var i=line.IndexOf(" ");
            int ndx2 = line.IndexOf(" ", ndx+key.Length+1, StringComparison.InvariantCultureIgnoreCase);
            if (ndx2 == -1)
                ndx2 = line.Length - 1;
            return line.Substring(ndx + key.Length, ndx2 - ndx - key.Length).Trim();
        }
        return "";
    }

结果:

在此处输入图片说明

下面的示例显示的是从文本中解析数据。 我只做名字和地位。 您可以使用代码来引用它。

 public static class Program
 {
    static void Main(string[] args)
    {

            // 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(@"PAth\Data.txt");

            // Display the file contents to the console. Variable text is a string.
            System.Console.WriteLine("Contents of WriteText.txt = {0}", text);

            string[] splited;
            string Name=string.Empty;
            string Status=string.Empty;

            splited = text.Split(new string[] { "Name:", "Name:" }, 
                        StringSplitOptions.None);
                       Console.WriteLine(splited[1]);
            Name = splited[1].Replace(System.Environment.NewLine, string.Empty); 

            splited = text.Split(new string[] { "Name:", "Deployment Status:" } 
                       ,StringSplitOptions.None);
                       Console.WriteLine(splited[1]);
            Status = splited[1].Replace(System.Environment.NewLine, string.Empty); 



        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM