Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,038

HOME > .NET Framework > Forum > สอบถามเรื่องการ replace ข้อมูลลงไปใน textfile อะครับ พอดี มันไม่ยอมเข้าไปในตำแหน่งที่ผมเขียนไว้


 

[.NET] สอบถามเรื่องการ replace ข้อมูลลงไปใน textfile อะครับ พอดี มันไม่ยอมเข้าไปในตำแหน่งที่ผมเขียนไว้

 
Topic : 114502



โพสกระทู้ ( 90 )
บทความ ( 0 )



สถานะออฟไลน์



Code (C#)
001.using System;
002.using System.Linq;
003.using System.Collections.Generic;
004.using System.ComponentModel;
005.using System.Data;
006.using System.Drawing;
007.using System.Text;
008.using System.Windows.Forms;
009.using System.IO;
010.using System.Globalization;
011. 
012.namespace ExcellenceSportHH
013.{
014.    public partial class frmCheckingSheet : Form
015.    {
016.        public frmCheckingSheet()
017.        {
018.            InitializeComponent();
019.        }
020. 
021.        private string _ValueLocationID;
022.        private string _ValueNameCheck;
023.        public string ValueLocationID
024.        {
025.            get
026.            {
027.                return this._ValueLocationID;
028.            }
029.            set
030.            {
031.                this._ValueLocationID = value;
032.            }
033.        }
034. 
035.        public string ValueNameCheck
036.        {
037.            get
038.            {
039.                return this._ValueNameCheck;
040.            }
041.            set
042.            {
043.                this._ValueNameCheck = value;
044.            }
045.        }
046. 
047.        public void LoadInfor()
048.        {
049.            StreamReader sr = new StreamReader("\\Program Files\\ExcellenceSportHH\\CheckStock.txt");
050.                       
051.                                string[] split = new string[11];
052.                                split = sr.ReadLine().Split(',');
053.            txtbarcode.Focus();
054.           
055.            
056.            
057.        }
058. 
059.        public void btnSave_Click(object sender, EventArgs e)
060.        {
061.            try
062.            {
063.                if (txtbarcode.Text == "")
064.                {
065.                    MessageBox.Show("Input your Barcode");
066.                    txtbarcode.Focus();
067.                }
068. 
069.                if (txtbarcode.Text != "")
070.                {
071.                    //StreamReader sr = new StreamReader("\\Program Files\\ExcellenceSportHH\\CheckStock.txt");
072.                    try
073.                    {
074.                        StreamReader sr = new StreamReader("\\Program Files\\ExcellenceSportHH\\CheckStock.txt");
075.                        do while (sr.Peek() > -1)
076.                            {
077.                                string[] split = new string[11];
078.                                split = sr.ReadLine().Split(',');
079.                                List<string> updateLines = new List<string>();
080.                               foreach (var line in sr.ReadLine())
081.                                {
082.                                    if (split[0] == txtbarcode.Text)
083.                                    {
084.                                        //update value จะให้ข้อมูลไปใส่แทนที่ในที่ครับ
085.                                        split[8] = (int.Parse(split[8]) + 1).ToString();
086.                                        split[9] = _ValueNameCheck;
087.                                        split[10] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
088.                                         
089.                                    }
090.                                    sr.Close();
091.แล้วค่ามันไม่ยอม update
092.                                    updateLines.Add(string.Join(",", split));
093.                                    sr.Close();
094.                                }                             
095.                            } while (sr.ReadLine() != null);
096.                        sr.Close();
097.                    }
098.                    catch (Exception ex) { MessageBox.Show(ex.ToString()); }
099.                    MessageBox.Show("Complete...");
100. 
101.                    txtbarcode.Text = "";
102.                    txtVN.Text = "";
103.                    txtColor.Text = "";
104.                    txtSize.Text = "";
105.                    txtStyle.Text = "";
106.                    txtCollection.Text = "";
107.                    txtPrice.Text = "";
108.                    txtQtyAll.Text = "";
109.                    txtbarcode.Focus();
110.                }
111.            }
112.            catch (Exception ex)
113.            {
114.                MessageBox.Show(ex.ToString());
115.            }
116.        }
117. 
118.        private void mnuLogout_Click(object sender, EventArgs e)
119.        {
120.            frmLogin backward = new frmLogin();
121.            backward.Show();
122.            this.Close();
123.        }
124. 
125.        private void txtbarcode_KeyDown(object sender, KeyEventArgs e)
126.        {
127.            if (e.KeyCode == Keys.Enter)
128.            {
129.                if (txtbarcode.Text != "")
130.                {
131.                    string[] split = new string[11];
132.                    //StreamReader sr = new StreamReader("\\Program Files\\CCL_ExcellenceSport\\CheckStock.txt");
133.                    StreamReader sr = new StreamReader("\\Program Files\\ExcellenceSportHH\\CheckStock.txt");
134. 
135.                    try
136.                    {
137.                        do while (sr.Peek() > -1 )
138.                        {
139.                            split = sr.ReadLine().Split(',');
140.                            string barcode = split[0];
141.                            if (string.Compare(barcode, txtbarcode.Text.Trim(), true) == 0)
142.                            {
143.                                //txtbarcode.Text = Barcode;
144.                                txtVN.Text = split[1];
145.                                txtStyle.Text = split[2];
146.                                txtColor.Text = split[3];
147.                                txtSize.Text = split[4];
148.                                txtPrice.Text = split[5];
149.                                txtCollection.Text = split[6];
150.                                txtQtyAll.Text = split[7];
151.                                btnSave.Focus();
152.                            }
153.                        } while (sr.ReadLine() != null);
154.                    }
155.                    catch (Exception ex)
156.                    {
157.                        MessageBox.Show(ex.ToString());
158.                    }
159.                }
160.                else
161.                {
162.                    MessageBox.Show("Not found this barcode, Plese check again!!");
163.                    txtbarcode.Focus();
164.                    txtbarcode.SelectAll();
165.                }
166.            }
167.        }
168. 
169.        private void mnuReport_Click(object sender, EventArgs e)
170.        {
171.            frmReport Nextpage = new frmReport();
172.            Nextpage.LocationCode = this._ValueLocationID;
173.            Nextpage.Show();
174.            this.Hide();
175.        }
176. 
177.        private void mnuExit_Click(object sender, EventArgs e)
178.        {
179.            Application.Exit();
180.        }
181. 
182.        private void frmCheckingSheet_Load(object sender, EventArgs e)
183.        {
184.            CultureInfo us = System.Globalization.CultureInfo.GetCultureInfo("en-US");
185.        }
186.    }
187.}




Tag : .NET, C#, VS 2008 (.NET 3.x)

Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2015-02-18 14:05:19 By : omyam001 View : 925 Reply : 4
 

 

No. 1



โพสกระทู้ ( 4,440 )
บทความ ( 23 )



สถานะออฟไลน์
Facebook

ถ้าเป็น text file ผมทำคล้ายๆกะบ ini ไฟล์ครับ
ลองเอาไปเล่นดูว่าได้รึป่าวนะครับ

Code (C#)
01.public static class clsINI
02.   {
03.       [DllImport("kernel32.dll", EntryPoint = "GetPrivateProfileStringA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
04.       private static extern int GetPrivateProfileString(string lpApplicationName, string lpKeyName, string lpDefault, global::System.Text.StringBuilder lpReturnedString, int nSize, string lpFileName);
05.       [DllImport("kernel32.dll", EntryPoint = "WritePrivateProfileStringA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
06.       private static extern int WritePrivateProfileString(string lpApplicationName, string lpKeyName, string lpString, string lpFileName);
07.       public static string ReadValue(string Path, string section, string key)
08.       {
09.           global::System.Text.StringBuilder sb = new global::System.Text.StringBuilder(255);
10.           dynamic i = GetPrivateProfileString(section, key, "", sb, 255, Path);
11.           return sb.ToString();
12.       }
13.       public static void WriteValue(string Path, string section, string key, string value) { WritePrivateProfileString(section, key, value, Path); }
14.           public static string textFileReader(string pathFileName)
15.  {
16.    string line;
17.    StreamReader fs;
18.    try
19.    {
20.       fs = new StreamReader(pathFileName);
21.       line = fs.ReadToEnd();
22. 
23.       /* อ่าน Encode จาก String ที่อ่านมาได้จาก text file */
24.       Encoding encodeSource = Encoding.GetEncoding(fs.CurrentEncoding.CodePage);
25.       fs.Close();
26. 
27.       //* ............ */
28.       Encoding systemEncode = Encoding.Default;
29.       Encoding targetEncode = encodeSource;
30. 
31.       /* สั่ง getbyte array จาก string ที่เราอ่านมา */
32.       byte[] srcData = systemEncode.GetBytes( line );
33.       byte[] dstData;
34. 
35.       /* ถ้าเป้น Encode ต่างกัน windows และ text file ให้ Convert byte array ไปเป็น text file encode */
36.       if( targetEncode != systemEncode )
37.         dstData = Encoding.Convert( systemEncode, targetEncode, srcData );
38.       else
39.         dstData = srcData;
40. 
41.       /* convert bytearray ไปเป็น string ด้วย text file encode */
42.       return  targetEncode.GetString(dstData);
43. 
44.    }
45.    catch(Exception ex)
46.    {
47.       throw new IOException("cannot find " + pathFileName,ex);
48.    }
49. 
50.  }
51.       public static System.Collections.Generic.List<string> textFileReaderFormline(string pathFileName)
52.       {
53. 
54.           System.Collections.Generic.List<string> list = new System.Collections.Generic.List<string>();
55.           System.IO.StreamReader fs;
56.           fs = new System.IO.StreamReader(pathFileName, System.Text.Encoding.GetEncoding(874));
57. 
58.           string line;
59.           while ((line = fs.ReadLine()) != null)
60.           {
61.               list.Add(line);
62.           }
63.           return list;
64. 
65.       }
66.   }

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2015-02-18 14:27:03 By : lamaka.tor
 

 

No. 2



โพสกระทู้ ( 90 )
บทความ ( 0 )



สถานะออฟไลน์


ของผมทำเป็น Smart device นะครับ พอดีลืมบอกไปครับ ติดปัญหา แค่ แทนที่ทำเดิมใน text ไม่ได้ แค่นั้นเองครับ รบกวนช่วยหน่อบนะครับ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2015-02-18 17:08:44 By : omyam001
 

 

No. 3



โพสกระทู้ ( 93 )
บทความ ( 0 )



สถานะออฟไลน์


ที่ผมดู code ผมยังไม่เห็น Code ที่จะใช้เขียนข้อมูลลง textfile เลยนะคับ พวก streamwriter เห็นแต่ streamreader

มันก็น่าจะไม่มีอะไรลง text file

ที่เห็นมีแต่การเก็บข้อมูลลง array

ผิดพลาดขออภัย คับผม
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2015-02-19 08:54:10 By : poomberry
 

 

No. 4



โพสกระทู้ ( 4,440 )
บทความ ( 23 )



สถานะออฟไลน์
Facebook

ตอบความคิดเห็นที่ : 2 เขียนโดย : omyam001 เมื่อวันที่ 2015-02-18 17:08:44
รายละเอียดของการตอบ ::
พอดีเห็นว่า StreamReader มันชอบ Error บ่อยอ่าครับ
ผมก็เลยไม่แนะนำ
แต่ถ้าเป็นแบบที่ผมทำจะไม่มี error

โค๊ดมันบ้านๆก็จริงแต่ไม่เคยมีปัญหากับ text file เลย


แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2015-02-19 09:21:13 By : lamaka.tor
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : สอบถามเรื่องการ replace ข้อมูลลงไปใน textfile อะครับ พอดี มันไม่ยอมเข้าไปในตำแหน่งที่ผมเขียนไว้
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)





ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2025 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่