|
 |
|
สอบถามวิธีที่ทำให้โปรแกรม อ่านชื่อไฟล์แบบไม่fixว่าไฟล์นั้นชื่ออะไรและวนหลูบหาชื่อไฟล์หน่อยครับ |
|
 |
|
|
 |
 |
|
คือตอนนี้ ผมใช้การอ่านไฟล์ แบบfix
Code (C#)
private void button1_Click(object sender, EventArgs e)
{
try
{
var lines = File.ReadAllLines("D:\\A60118072.txt");
DataTable dt = new DataTable();
if (lines.Length != 0)
{
foreach (string columnName in lines.FirstOrDefault().Split(' '))
{
dt.Columns.Add(columnName);
}
foreach (var cellValues in lines.Skip(1))
{
DataRow dr = dt.NewRow();
var cellArray = cellValues.Substring(0, 10);
var cellarray1 = cellValues.Substring(10, 6);
var cellarray2 = cellValues.Substring(16, 3);
var cellarray3 = cellValues.Substring(19, 1);
var cellarray4 = cellValues.Substring(20, 151);
var cellarray5 = cellValues.Substring(0, 10);
var cellarray6 = cellValues.Substring(10, 19);
var cellarray7 = cellValues.Substring(29, 3);
var cellarray8 = cellValues.Substring(32, 40);
var cellarray9 = cellValues.Substring(72, 40);
var cellarray10 = cellValues.Substring(112, 3);
var cellarray11 = cellValues.Substring(115, 19);
var cellarray12 = cellValues.Substring(134, 1);
var cellarray13 = cellValues.Substring(135, 19);
var cellarray14 = cellValues.Substring(154, 1);
var cellarray15 = cellValues.Substring(155, 20);
var cellarray16 = cellValues.Substring(175, 1);
var cellarray17 = cellValues.Substring(176, 13);
dt.Rows.Add(cellArray, cellarray1, cellarray2, cellarray3, cellarray4, cellarray5, cellarray6, cellarray7, cellarray8, cellarray9, cellarray10, cellarray11, cellarray12, cellarray13, cellarray14, cellarray15, cellarray16, cellarray17);
}
Encoding utf16 = Encoding.GetEncoding(874);
byte[] output = utf16.GetBytes(dt.ToString());
}
dataGridView1.DataSource = dt;
}
catch (Exception ex)
{
AutoClosingMessageBox.Show(ex.Message, "Error", 2000);
}
}
สมมุติว่าผมมีไฟล์ txt ที่ข้อมูลเหมื่อนกันแต่ชื่อไฟล์ต่างกัน เช่น A60118072.txt , A60118073.txt , A60118074.txt
อยากจะให้มันค้นหาไฟล์ txt เองแล้วมาโชว์อะครับ
รบกวนหน่อยครับ
Tag : .NET, Win (Windows App), C#

|
|
 |
 |
 |
 |
Date :
2017-08-08 10:29:31 |
By :
darkgolfman0 |
View :
807 |
Reply :
6 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (C#)
System.IO.Directory.GetFiles(@"Path", "A*.txt", System.IO.SearchOption.TopDirectoryOnly).ToList<string>()
.ForEach(f =>
{
//go to read file f
});
ลองใช้ regex ดูดีไม๊ครับ ดีกว่าการ Split ของ string เพราะสารมารถใช้ \s{1,} แทน ' ' ที่เราไม่รู้จำนวนได้
|
ประวัติการแก้ไข 2017-08-08 10:40:26
 |
 |
 |
 |
Date :
2017-08-08 10:34:23 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ไฟล์แรก ชื่อไฟล์ A6011808.txt
2017/08/18000001579N
00000000010000000000000000000579ธรรมรัตน์ ธรรมาชีวะ THAMMARAT THAMMACHEWA NOR5796179989200000001S5796179989300000001D5264131300726679 3100602471412
00000000060000000000000000000579สมพิศ ป้อมจักรศิลป์ SOMPIS POMJAKSIN NOR5796179989200000006S5796179989300000006D5264131300726687 3100602515274
00000000020000000000000000000579รัชต์กิตตน์ จิรปรีชานนท์ RATCHAKIT JIRAPRECHANOON NOR5796179989200000002S5796179989300000002D5264131300726695 3100903002853
00000000470000000000000000000579จักรเพชร อนุศาสน์สถิตชัย JUKPECH ANUSASSATITCHAI NOR5796179989200000047S5796179989300000047D5264131300726703 3100904736834
ไฟล์สอง ชื่อไฟล์ A6011908.txt
2017/08/19000001579N
00000000520000000000000000000579เฉิดโฉม ชอคเกิล CHIRTCHOM SCHOGGL NOR5796179989200000052S5796179989300000052D5264131300726737 3110102001891
00000001090000000000000000000579ธีรภัทร คลังธนกูล TEERAPAT KANGTANAKOOL NOR5796179989200000109S5796179989300000109D5264131300726745 3100700316934
00000000070000000000000000000579สมมิตร เหลืองวรา SOMMIT LOUNGWARA NOR5796179989200000007S5796179989300000007D5264131300726752 3209900142171
00000000090000000000000000000579ชญานิศ นิ่มนวล CHAYANIT NIMNAUN NOR5796179989200000009S5796179989300000009D5264131300726760 3120100976097
|
 |
 |
 |
 |
Date :
2017-08-08 11:02:51 |
By :
darkgolfman0 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
1. โหลด regex hero มาติดตั้งก่อน
http://regexhero.net/tester/
2. ศึกษาวิธีการใช้งาน Regular Expressions อันที่จริงศึกษาจาก regex hero โดยตรงเลยก็ได้
จากโค้ดจะได้ประมาณนี้
Code (C#)
string strRegex = @"(\d{1,})\s{0,}(.*?)\s{1,}([A-Z\s]{1,})([A-Z\d]{1,})\s{0,}(\d{1,})";
Regex myRegex = new Regex(strRegex, RegexOptions.None);
string strTargetString = @"2017/08/18000001579N" + "\n" + @"00000000010000000000000000000579ธรรมรัตน์ ธรรมาชีวะ THAMMARAT THAMMACHEWA NOR5796179989200000001S5796179989300000001D5264131300726679 3100602471412" + "\n" + @"00000000060000000000000000000579สมพิศ ป้อมจักรศิลป์ SOMPIS POMJAKSIN NOR5796179989200000006S5796179989300000006D5264131300726687 3100602515274" + "\n" + @"00000000020000000000000000000579รัชต์กิตตน์ จิรปรีชานนท์ RATCHAKIT JIRAPRECHANOON NOR5796179989200000002S5796179989300000002D5264131300726695 3100903002853" + "\n" + @"00000000470000000000000000000579จักรเพชร อนุศาสน์สถิตชัย JUKPECH ANUSASSATITCHAI NOR5796179989200000047S5796179989300000047D5264131300726703 3100904736834";
foreach (Match myMatch in myRegex.Matches(strTargetString))
{
if (myMatch.Success)
{
// แล้วแต่จะเอาไปใช้งาน
}
}

โค้ดบ้านๆก็ประมาณนี้ครับ
Code (C#)
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
TestRegexFormTXT("D:\\A6011808.txt");
}
void TestRegexFormTXT(string f_txt)
{
string strRegex = @"(\d{1,})\s{0,}(.*?)\s{1,}([A-Z\s]{1,})([A-Z\d]{1,})\s{0,}(\d{1,})";
System.Text.RegularExpressions.Regex myRegex = new System.Text.RegularExpressions.Regex(strRegex, System.Text.RegularExpressions.RegexOptions.None);
System.IO.File.ReadAllLines(f_txt, System.Text.Encoding.GetEncoding(874)).ToList<string>()
.ForEach(l => {
foreach (System.Text.RegularExpressions.Match myMatch in myRegex.Matches(l))
{
MessageBox.Show(
myMatch.Groups[1].Value + Environment.NewLine +
myMatch.Groups[2].Value + Environment.NewLine +
myMatch.Groups[3].Value + Environment.NewLine +
myMatch.Groups[4].Value + Environment.NewLine +
myMatch.Groups[5].Value + Environment.NewLine);
}});
}
}
ส่วนไฟล์สามารถ for ได้
Code (C#)
System.IO.Directory.GetFiles(@"D:\Filetxt", "A60*.txt", System.IO.SearchOption.TopDirectoryOnly).ToList<string>()
.ForEach(f => TestRegexFormTXT(f));
ส่วน regex อาจจะต้องปรับแก้หน่อย นะครับ ผมทำไว้แค่เป็นตัวอย่าง
|
 |
 |
 |
 |
Date :
2017-08-08 11:33:44 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 01
|