Click here to Skip to main content
15,889,808 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i try this code when any new file is created in folder then i fetch date and time
so now i also want refresh that folder

What I have tried:

C#
while (true)
            {
                try
                {  
                    
                    
                    string abc, def= "";
                    abc= ConfigurationManager.AppSettings.Get("apath");
                    def= ConfigurationManager.AppSettings.Get("dpath");
                    
                    DateTime dt1= File.GetLastAccessTime(abc);
                    DateTime dt2= File.GetLastAccessTime(def);
                    
                    Console.WriteLine("the last access time for  abc{0}", dt1);
                    Console.WriteLine("the last access time for def{0}", dt2);

                    TimeSpan timediff = DateTime.Now - dt1;
                    TimeSpan timediff1 = DateTime.Now - dt2;
                    // DateTime.Now.Subtract(teltonika).Minutes > 5

                    if( timediff.TotalMinutes > 5)
                    {
                        DataClasses1DataContext db = new DataClasses1DataContext();
                        var  u = db.tbl_urgent_contacts;
                        foreach (var a in u)
                        {
                            tbl_OutBox tb = new tbl_OutBox();
                            tb.FromSIM_No = a.SimNo;
                            tb.ToSIM_No = a.SimNo;
                            tb.ToText = "Check abc";
                            tb.Reply = "NA";
                            tb.Response = "NA";
                            tb.RegNo = "NA";
                            tb.Datetd = DateTime.Now;
                            tb.FFID = "NA";
                            tb.UserId = "You";
                            tb.FromText = "Check abc";
                            db.tbl_OutBoxes.InsertOnSubmit(tb);
                            db.SubmitChanges();
                        } 
                    }
                    if (timediff1.TotalMinutes > 5)
                    {
                        DataClasses1DataContext db = new DataClasses1DataContext();
                        var u = db.tbl_urgent_contacts;
                        foreach (var a in u)
                        {
                            tbl_OutBox tb = new tbl_OutBox();
                            tb.FromSIM_No = a.SimNo;
                            tb.ToSIM_No = a.SimNo;
                            tb.ToText = "Check def";
                            tb.Reply = "NA";
                            tb.Response = "NA";
                            tb.RegNo = "NA";
                            tb.Datetd = DateTime.Now;
                            tb.FFID = "NA";
                            tb.UserId = "You";
                            tb.FromText = "Check def";
                            db.tbl_OutBoxes.InsertOnSubmit(tb);
                            db.SubmitChanges();
                        }
                    }
                   
                }
                catch (Exception e)
                {
                    Console.WriteLine("The process failed: {0}", e.ToString());
                }
                Thread.Sleep(5000);
            }
        }


i access these folder in app.config
appSettings>
XML
<add key="apath" value="C:\Users\Administrator\Desktop\New folder" />
<add key="dpath" value="C:\Users\Administrator\Desktop\New folder" />


now i want to refresh this new folder through code
Posted
Updated 12-May-16 4:27am
v4
Comments
Sergey Alexandrovich Kryukov 12-May-16 8:44am    
What it that supposed to mean, "refresh folder"?
"Thread.Sleep(5000);" tells the tail: you have no clue what you are doing. Why sleeping?
—SA
Patrice T 12-May-16 16:14pm    
may be he need some rest to think abiyr what is wrong :-)
Sergey Alexandrovich Kryukov 12-May-16 17:31pm    
:-)
ZurdoDev 12-May-16 9:43am    
Your code hasn't opened a folder. You have to find the window that the user has open and then send a command to refresh it. You can google c# find open window for examples.

1 solution

First of all change in file system is an event. Checking it in cycle is bad idea. The better way is using event and process it as soon as it fires. So, you should redesign your solution.

Most likely FileSystemWatcher Class ( FileSystemWatcher Class (System.IO)[^] ) can help you.

And also. You should manage access rights properly, especially if you create ASP application. Improperly set access to folders could lead to denial of access or to security flaw (hacker can access your monitored folder).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900