如何从文本文件中提取文件路径(如果存在)或手动输入文件路径(如果不存在则使用C# [英] How to extract file path from text file if present or enter filepath manually if not present using C#

查看:88
本文介绍了如何从文本文件中提取文件路径(如果存在)或手动输入文件路径(如果不存在则使用C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含以下信息的文本文件:



I have a text file with information:

Template PUC
C# Assignment.
Path=E:\Project_Excel.xlsx
Path= EMailId=abc@xyz.com; def@xyz.com; ghi@xyz.com





我正在使用C#解析文本文件并查找Path =并在=之后提取路径 然后打开它进一步操作。如果Path =没有提到路径,那么它应该要求用户在控制台窗口上手动输入路径。任何人都可以在我出错的地方帮助我吗?



我尝试了什么:





I am parsing the text file using C# and looking for "Path=" and extract the path after "=" and open it for further operation. If "Path=" has no path mentioned then it should ask user to enter the path manually on console window. Can anyone please help me where I am going wrong?

What I have tried:

static void Main(string[] args)
        {
            try
            {
                string file = "E:\\Project_PU.txt";
                StreamReader sr = new StreamReader(file);
                String lines = sr.ReadToEnd();  

                int x = lines.IndexOf('=');
                int y = lines.IndexOf('_');
                string x = lines.Substring(x + 1, y - x);


                string mail = File.ReadAllLines(file).FirstOrDefault(line => line.StartsWith("EmailId="));
                string sString = mail;
                string[] sLines = sString.Split(new string[] { "EmailId=", ";" }, StringSplitOptions.RemoveEmptyEntries);


                Excel.Application xlApp;
                Excel.Workbook xlWorkBook;
                Excel.Worksheet xlWorkSheet;
                Excel.Range range;

                long rowCount;
                long rw = 0;
                long cl = 0;

                xlApp = new Excel.Application();

                string path = "";
                Console.WriteLine();
                Console.WriteLine();


                if (File.Exists(path))
                {

                    xlWorkBook = xlApp.Workbooks.Open(path, 0, true, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);

推荐答案

这里有问题:你的EmailId部分不起作用,因为你文件中的文字很糟糕而且EmailId =不在行的开头。

所以首先仔细检查你的文件格式 - 它可能不应该是这样,但是像这样:

You have problems here: your EmailId part doesn't work because the text in your file is bad and "EmailId=" is not at the start of the line.
So start by double checking your file format - it probably shouldn't look like that, but like this:
Template PUC
C# Assignment.
Path=E:\Project_Excel.xlsx
Path= 
EMailId=abc@xyz.com; def@xyz.com; ghi@xyz.com



假设,然后尝试处理每一行,而不只是寻找电子邮件地址:


Assuming that, then try processing each line instead of just looking for the email addresses:

string[] lines = File.ReadAllLines(file);
string path = "";
string[] emails = new string[0];
foreach (string line in lines)
    {
    string[] parts = line.Split('=');
    if (parts.Length == 2)
        {
        string type = parts[0].Trim().ToLower();
        string data = parts[1].Trim();
        switch (type)
            {
            case "path":
                if (data != "")
                    {
                    path = data;
                    }
                break;
            case "emailid":
                emails = data.Split(';');
                break;
            }
        }
    }

最后,如果路径不包含一个有效的路径,询问用户。

At the end of that, if path does not contain a valid path, ask the user.


这篇关于如何从文本文件中提取文件路径(如果存在)或手动输入文件路径(如果不存在则使用C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆