 |
|
จะทำการ Check text file ว่าเปิดอยู่ไหม่อ่าครับ ถ้าเปิดอยู่ให้ปิด |
|
 |
|
|
 |
 |
|
เปิดอยู่นี่ด้วย Process หรือว่า Program อื่น ๆ ครับ ถ้า Process ให้ Close เองเลยครับ โดยใช้ try { }
|
 |
 |
 |
 |
Date :
2016-07-18 11:12:30 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตอบความคิดเห็นที่ : 3 เขียนโดย : lamaka.tor เมื่อวันที่ 2016-07-18 14:12:51
รายละเอียดของการตอบ ::
Code (C#)
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Configuration;
using System.Text.RegularExpressions;
using System.Diagnostics;
namespace CheckStock1
{
public partial class UNF : Form
{
public string A { get; set; }
public string B { get; set; }
public UNF(string data, string data2)
{
InitializeComponent();
A = data;
B = data2;
}
string log = "CheckStock.txt";
private void UNF_Load(object sender, EventArgs e)
{
List<ReportUNF> reportList = new List<ReportUNF>();
DataGridTableStyle tableS = new DataGridTableStyle();
tableS.MappingName = reportList.GetType().Name;
DataGridTextBoxColumn tbcName = new DataGridTextBoxColumn();
tbcName.MappingName = "UNF";
tbcName.HeaderText = "UNF";
tbcName.Width = 74;
tableS.GridColumnStyles.Add(tbcName);
DataGridTableStyle tableS1 = new DataGridTableStyle();
tableS.MappingName = reportList.GetType().Name;
DataGridTextBoxColumn tbcName1 = new DataGridTextBoxColumn();
tbcName1.MappingName = "AMOUNT";
tbcName1.HeaderText = "AMOUNT";
tbcName1.Width = 104;
tableS.GridColumnStyles.Add(tbcName1);
dataGrid1.TableStyles.Clear();
dataGrid1.TableStyles.Add(tableS);
dataGrid1.TableStyles.Add(tableS1);
StreamReader sr = new StreamReader(log);
if (File.Exists(log) == false)
{
return;
}
sr = new StreamReader(log);
string line1;
ReportUNF reportUnf;
string unf;
string dateTime;
string orderNo;
string dat = "";
string containerNo;
string serial1;
while ((line1 = sr.ReadLine()) != null)
{
int tab_target = line1.Split('\t').Length - 2;
int start = 0;
dateTime = line1.Substring(start, line1.IndexOf('\t', start) - start);
dat = dateTime + '\t';
start = line1.IndexOf('\t', 0) + 1;
orderNo = line1.Substring(start, line1.IndexOf('\t', start) - start);
dat += orderNo + '\t';
start = line1.IndexOf('\t', start) + 1;
containerNo = line1.Substring(start, line1.IndexOf('\t', start) - start);
dat += containerNo + '\t';
start = line1.IndexOf('\t', start) + 1;
serial1 = line1.Substring(start, line1.IndexOf('\t', start) - start);
dat += serial1 + '\t';
start = line1.IndexOf('\t', start) + 1;
unf = line1.Substring(start, line1.IndexOf('\t', start) - start);
dat += unf + '\t';
start = line1.IndexOf('\t', start) + 1;
if (orderNo == A && containerNo == B)
{
if (reportList.Where(v => v.UNF == unf).FirstOrDefault() != null)
{
foreach (var r in reportList.Where(v => v.UNF == unf))
{
r.AMOUNT += 1;
}
}
else
{
reportUnf = new ReportUNF();
reportUnf.UNF = unf;
reportUnf.AMOUNT += 1;
reportList.Add(reportUnf);
}
}
}
this.dataGrid1.DataSource = reportList;
sr.Close();
}
private void btn_Menu_Click(object sender, EventArgs e)
{
Menu m1 = new Menu();
m1.Show();
this.Hide();
}
}
public class ReportUNF
{
public virtual string UNF { get; set; }
public virtual int AMOUNT { get; set; }
}
}
คือตัว text file มันยังทำงานอยู่อ่าครับ หลังจากออกจากหน้านี้ไป พอไปเช็คเพื่อจะเขียนข้อมูลต่อจากของเดิมAppendTextแล้วมันฟ้องว่า file text ยัง process อยู่อ่าครับ
|
 |
 |
 |
 |
Date :
2016-07-25 16:04:17 |
By :
bankzbox1 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตอบความคิดเห็นที่ : 4 เขียนโดย : bankzbox1 เมื่อวันที่ 2016-07-25 16:04:17
รายละเอียดของการตอบ ::
เป็นความจริงอยู่แล้วครับ(ไม่มีข้อโต้แย้ง 5555)
ถ้าใช้
Code
Using(StreamReader sr = new StreamReader(path))
{
//จาดปาย ห้ามถามนะว่าเอาโค๊ดไหนมาลง
//ก็เอาโค๊ดที่เราต้องการจะให้จัดการกับ txt น่านแหละ
}
มันก็จะไม่ฟ้องให้รำคาญใจอีก
ปล. Using สามารถใช้ได้กับหลายๆกรณีนะครับ
อย่างคนที่ชอบมีปัญหาเปิด connect ต่างๆค้างไว้แล้วต้องมานั่งงมว่าตรูเปิดหรือปิดอยู่หว่า
ก็ใช้
Code
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
// Do work here; connection closed on following line.
}
เอาแบบบ้านๆตามที่ชาวบ้านอย่างผมเข้าใจก็คือ
Code
using (Object obj= new Object )
{
// เอาที่ท่านสะบายใต
}
แค่นี้ก็ไม่ต้องสนใจเรื่อง ลักปิดลักเปิด อีกต่อไปครับ
|
 |
 |
 |
 |
Date :
2016-07-25 17:04:48 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
นี่เป็นตัวอย่างการโหลด Lyric ของเว็บๆหนึ่งผม save ลงเป็น txt
ยังไม่ค่อยสูมบูรณ์เพราะยังแก้ปัญหาเรื่อง thread ไม่ดีเท่าที่ควร
แต่ในแง่ของเรื่อง txt file ผมถือว่าใช้ได้เลยทีเดียว
Code (C#)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.IO;
namespace A_Z_Lyric
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string fileErr = @"D:\A-Z Lyrics\Err.txt";
List<string> lst = new List<string>{"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","19"};
void GetArtist(string url)
{
if (url.Length <= 0) return;
string html = url.toHTML();
foreach (System.Text.RegularExpressions.Match match in new Regex(@"<a href=""(.*?/.*?.html)"">.*?</a><br>",RegexOptions.None).Matches(html))
{
GetSongbyArtist( "http://www.azlyrics.com/"+ match.Groups[1].Value );
System.Threading.Thread.Sleep(5000);
}
}
void GetSongbyArtist(string url)
{
// MessageBox.Show(url);
if (url.Length <= 0) return;
string html = url.toHTML();
string artist = System.IO.Path.GetFileNameWithoutExtension(url);
foreach (System.Text.RegularExpressions.Match match in new Regex(@"<a href=""\.\.(/lyrics/.*?.html)"" target=""_blank"">.*?</a><br>", RegexOptions.None).Matches(html))
{
new System.Threading.Thread(GetLyric).Start(new object[] { "http://www.azlyrics.com" + match.Groups[1].Value });
System.Threading.Thread.Sleep(3000);
}
}
void GetLyric(object obj)
{
string url = (string)((object[])obj)[0];
if (url.Length <= 0 ) return;
try
{
string ss = "<!-- Usage of azlyrics.com content by any third-party lyrics provider is prohibited by our licensing agreement. Sorry about that. -->";
string sf = "<!-- MxM banner -->";
string html = url.toHTML();
Regex regSong = new Regex(@"<title>(.*?)\s?LYRICS\s?-\s?(.*?)</title>", RegexOptions.None);
string artist = regSong.Matches(html)[0].Groups[1].Value.Trim();
string songname = new Regex(@"(\W)", RegexOptions.None).Replace(regSong.Matches(html)[0].Groups[2].Value, " ").Trim();
int iS = html.IndexOf(ss) + ss.Length;
int iF = (html.IndexOf(sf) < 0) ? html.Length : html.IndexOf(sf);
string s = "artist:" + artist + Environment.NewLine + "songname:" + songname + Environment.NewLine + Environment.NewLine +
new Regex(@"(</?\w{1,10}>)", RegexOptions.None).Replace(html.Substring(iS, iF - iS).Replace("<br>", "\n"), "");
string path = @"D:\A-Z Lyrics\" + artist;
if (!System.IO.Directory.Exists(path)) { System.IO.Directory.CreateDirectory(path); }
string file = path + "\\" + artist + "@"+ songname + ".txt";
if (!File.Exists(file))
{
using (StreamWriter sw = File.CreateText(file)) { sw.Write(s); }
using (StreamWriter sw = (!File.Exists(fileErr)) ? File.CreateText(fileErr) : new StreamWriter(fileErr, true)) { sw.WriteLine(url + ":Done"); }
this.toolStripStatusLabel1.Text = artist + "@" + songname;
}
}
catch { using (StreamWriter sw = (!File.Exists(fileErr)) ? File.CreateText(fileErr) : new StreamWriter(fileErr, true)) { sw.WriteLine(url + ":Err"); } }
System.Threading.Thread.Sleep(5000);
}
private void Form1_Load(object sender, EventArgs e)
{
foreach (string s in lst)
{
GetArtist( "http://www.azlyrics.com/"+s+".html");
System.Threading.Thread.Sleep(2000);
}
}
}
}
|
 |
 |
 |
 |
Date :
2016-07-25 17:13:16 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตอบความคิดเห็นที่ : 6 เขียนโดย : lamaka.tor เมื่อวันที่ 2016-07-25 17:13:16
รายละเอียดของการตอบ ::
Code (C#)
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Configuration;
using System.Text.RegularExpressions;
using System.Diagnostics;
namespace CheckStock1
{
public partial class UNF : Form
{
public string A { get; set; }
public string B { get; set; }
public UNF(string data, string data2)
{
InitializeComponent();
A = data;
B = data2;
}
string log = "CheckStock.txt";
private void UNF_Load(object sender, EventArgs e)
{
List<ReportUNF> reportList = new List<ReportUNF>();
DataGridTableStyle tableS = new DataGridTableStyle();
tableS.MappingName = reportList.GetType().Name;
DataGridTextBoxColumn tbcName = new DataGridTextBoxColumn();
tbcName.MappingName = "UNF";
tbcName.HeaderText = "UNF";
tbcName.Width = 74;
tableS.GridColumnStyles.Add(tbcName);
DataGridTableStyle tableS1 = new DataGridTableStyle();
tableS.MappingName = reportList.GetType().Name;
DataGridTextBoxColumn tbcName1 = new DataGridTextBoxColumn();
tbcName1.MappingName = "AMOUNT";
tbcName1.HeaderText = "AMOUNT";
tbcName1.Width = 104;
tableS.GridColumnStyles.Add(tbcName1);
dataGrid1.TableStyles.Clear();
dataGrid1.TableStyles.Add(tableS);
dataGrid1.TableStyles.Add(tableS1);
using (StreamReader sr = new StreamReader(log))
{
if (File.Exists(log) == false)
{
return;
}
string line1;
ReportUNF reportUnf;
string unf;
string dateTime;
string orderNo;
string dat = "";
string containerNo;
string serial1;
while ((line1 = sr.ReadLine()) != null)
{
int tab_target = line1.Split('\t').Length - 2;
int start = 0;
dateTime = line1.Substring(start, line1.IndexOf('\t', start) - start);
dat = dateTime + '\t';
start = line1.IndexOf('\t', 0) + 1;
orderNo = line1.Substring(start, line1.IndexOf('\t', start) - start);
dat += orderNo + '\t';
start = line1.IndexOf('\t', start) + 1;
containerNo = line1.Substring(start, line1.IndexOf('\t', start) - start);
dat += containerNo + '\t';
start = line1.IndexOf('\t', start) + 1;
serial1 = line1.Substring(start, line1.IndexOf('\t', start) - start);
dat += serial1 + '\t';
start = line1.IndexOf('\t', start) + 1;
unf = line1.Substring(start, line1.IndexOf('\t', start) - start);
dat += unf + '\t';
start = line1.IndexOf('\t', start) + 1;
if (orderNo == A && containerNo == B)
{
if (reportList.Where(v => v.UNF == unf).FirstOrDefault() != null)
{
foreach (var r in reportList.Where(v => v.UNF == unf))
{
r.AMOUNT += 1;
}
}
else
{
reportUnf = new ReportUNF();
reportUnf.UNF = unf;
reportUnf.AMOUNT += 1;
reportList.Add(reportUnf);
}
}
}
this.dataGrid1.DataSource = reportList;
sr.Close();
}
}
private void btn_Menu_Click(object sender, EventArgs e)
{
Menu m1 = new Menu();
m1.Show();
this.Hide();
}
}
public class ReportUNF
{
public virtual string UNF { get; set; }
public virtual int AMOUNT { get; set; }
}
}
ได้แล้วครับขอบคุนครับ
|
 |
 |
 |
 |
Date :
2016-07-26 10:15:54 |
By :
bankzbox1 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
   
|
 |
 |
 |
 |
Date :
2016-07-26 10:38:32 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|