Raushan Raj

Raushan Raj

  • NA
  • 23
  • 11.7k

How to extract file path from text file if present using C#

May 28 2017 1:12 AM
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
 
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?
  1. static void Main(string[] args)  
  2.         {  
  3.             try  
  4.             {  
  5.                 string file = "E:\\Project_PU.txt";  
  6.                 StreamReader sr = new StreamReader(file);  
  7.                 String lines = sr.ReadToEnd();    
  8.                   
  9.                 int x = lines.IndexOf('=');  
  10.                 int y = lines.IndexOf('_');  
  11.                 string x = lines.Substring(x + 1, y - x);  
  12.     
  13.   
  14.                 string mail = File.ReadAllLines(file).FirstOrDefault(line => line.StartsWith("EmailId="));  
  15.                 string sString = mail;  
  16.                 string[] sLines = sString.Split(new string[] { "EmailId="";" }, StringSplitOptions.RemoveEmptyEntries);  
  17.   
  18.   
  19.                 Excel.Application xlApp;  
  20.                 Excel.Workbook xlWorkBook;  
  21.                 Excel.Worksheet xlWorkSheet;  
  22.                 Excel.Range range;  
  23.   
  24.                 long rowCount;  
  25.                 long rw = 0;  
  26.                 long cl = 0;  
  27.   
  28.                 xlApp = new Excel.Application();  
  29.   
  30.                 string path = "";  
  31.                 Console.WriteLine();  
  32.                 Console.WriteLine();  
  33.   
  34.                   
  35.                 if (File.Exists(path))  
  36.                 {  
  37.                       
  38.                     xlWorkBook = xlApp.Workbooks.Open(path, 0, true, 5, """"true, Excel.XlPlatform.xlWindows, "\t"falsefalse, 0, true, 1, 0); 
 

Answers (1)