c# - Reading all text files in a network path in a WinForms app -
how can read text files in network path in c# winforms?. text files name changing.
example:
- abc-gd09538.txt
- adb-jk3949.txt
- gjd-kgl9495-txt
the format of each text file when opened is:
some text text text text [data start] "data1"|"data2"|"data3"|"data4"|"data5"|"data5"|"data6" [data end]
i ignore "some text" part of text file , proceed text file reading on line [data start] , end on line [data end] perform query database , insert data received text files table.
from msdn website: https://msdn.microsoft.com/en-us/library/dd997370%28v=vs.110%29.aspx
this enumerate through c:\ directory , provide list of each .txt file (i took out clause).
using system; using system.io; using system.linq; class program { static void main(string[] args) { try { var files = file in directory.enumeratefiles(@"c:\", "*.txt", searchoption.alldirectories) line in file.readlines(file) select new { file = file, line = line }; foreach (var f in files) { console.writeline("{0}\t{1}", f.file, f.line); } console.writeline("{0} files found.", files.count().tostring()); } catch (unauthorizedaccessexception uaex) { console.writeline(uaex.message); } catch (pathtoolongexception pathex) { console.writeline(pathex.message); } } }
to go through each file , read "data" information. make sure pass parameter 'f' if make separate function.
try { foreach (var f in files) { using (streamreader sr = new streamreader(f.file)) { string readline; { readline = sr.readline(); string[] readlinesplit = readline.split('|'); if (readlinesplit.length > 1) { //make call database query , update readsplit. } } while (!sr.endofstream); } } } catch (exception ex) { messagebox.show(ex.message); }
i suggest creating new thread on how make query , update database. make no mention on database is, table structure or otherwise.
be safe,
-rob
Comments
Post a Comment