2016-10-25 4 views
0
public ActionResult Upload(HttpPostedFileBase file) 
{ 

    var fileName = Path.GetFileName(file.FileName); 
    //file.SaveAs(Path.Combine(path, fileName)); 
    string[] lines = System.IO.File.ReadAllLines(file.FileName); 
    . 
    . 
    . 

     //} 
    return View("~/~/xxxDB/xxxxx"); 
} 

입력 TXT 파일 :C# 분할 txt 파일

LINE1: CLASS ABC 1 1 2 
LINE2: NAMES 
LINE3: INFO 0 0 0 0 
LINE4: KEY *NULL* 
LINE5: BCMD *NULL* 
LINE6: RCMD *NULL* 

열 이름 :

class_a 
class_b 
class_c 
class_d 
names_a 
info_a 
info_b 
info_c 
info_d 
. 
. 
. 

나는 코딩 논리가 남기려하기로 결심했다면 다음 문, 선이 같은 경우 클래스를 만들고 그 값을 데이터 테이블에 넣고 ""로 나눕니다. 내가 어떻게 할 수있어, 제발 도와 줘.

string line; 
StreamReader sr = new StreamReader(file.FileName); 
while ((line = sr.ReadLine()) != null) 
{ 
    string[] fields = line.Split(' '); 
    string classA = fields[0]; 
    string classB = fields[1]; 
    // and so on 
} 

sr.Close(); 

이는 것보다 한 번에 한 줄이 아니라 파일의 내용을 통해 루프 :

+0

Foreach (줄 단위로 var 줄)를 사용하여 줄을 반복 한 다음 다시 공백으로 분할합니다. var split = line.split (""); - 케이스 "INFO": case "KEY": case "BCMD"등을 전환 (split [0]) 할 수 있습니다. –

+0

txt 파일이 실제로'LINE1' ...'LINE2' ...로 시작합니까? –

+0

@TimSchmelter 예 – peter

답변

0

사용에서는 StreamReader 라인으로 TXT 라인을 읽을 수는

여기
using (StreamReader sr = new StreamReader(path)) 
     { 
      while (sr.Peek() >= 0) 
      { 
       Console.WriteLine(sr.ReadLine()); 
      } 
     } 
0

내가 어떻게 할 것입니다 ReadAllLines이 할 파일의 "slurping".