webForumDet fria alternativet

FileSystemWatcher

1 svar · 405 visningar · startad av Zaiman

ZaimanMedlem sedan dec. 20014 239 inlägg
#1

Hjälp.. missar något.. Kollar en katalog efter fil och triggar på om den skapas eller ändras. Men den loopar 4 ggr.. Varför?

Det finns bara en rad i filen som den läser.

private void DoLabel(object sender, FileSystemEventArgs e)
        {
            string filename = "C:\\pickup1\\jojo.dat";
            string line;
            string[] myArr;

            using (TextReader tx = new StreamReader(filename, System.Text.Encoding.Default, false))
            {
                while ((line = tx.ReadLine()) != null)
                {
                    line = line.Replace('\"', ' ');
                    myArr = line.Split(new Char[] { ',' });
                    if (myArr[0].Trim() == "H")
                    {
                        headerText = myArr[2].ToString();
                        ordernrText = myArr[1].ToString();
                        HeaderLabel.OriginAtMargins = false;
                        HeaderLabel.PrinterSettings.Copies = 1;
                        MessageBox.Show(myArr[0].Trim().ToString(), "HEADER", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                        //HeaderLabel.Print();
                    }
                    else
                    {
                        picturePath = "C:\\Pictures\\" + myArr[6].Replace(" ", "") + ".gif";
                        slrText = myArr[5].ToString();
                        benamning = myArr[3].TrimStart().ToString();
                        JokerEtikett.OriginAtMargins = false;
                        JokerEtikett.PrinterSettings.Copies = 1;
                        JokerEtikett.Print();

                    }

                }
                tx.Close();
                tx.Dispose();
               
            }
           
                toolStripStatusLabel1.Text = "Senaste körning: " + GetTime();
            
        }
ZaimanMedlem sedan dec. 20014 239 inlägg
#2

Saxat från MSDN:
Common file system operations might raise more than one event. For example, when a file is moved from one directory to another, several OnChanged and some OnCreated and OnDeleted events might be raised. Moving a file is a complex operation that consists of multiple simple operations, therefore raising multiple events. Likewise, some applications (for example, antivirus software) might cause additional file system events that are detected by FileSystemWatcher.

Alltså kan eventet avfyras fler gånger..

Byter lösning till en timer med diverse annan kod.

124 ms totalt · 3 externa anrop · v20260731065814-full.30151723
0 ms — hämta forumlista (cache)
0 ms — hämta statistik (cache)
121 ms — hämta tråd, inlägg och bilagor (db)