Reply to thread

I am working on an application that download .db from a remote server to a local server. These files are in .db format, and they need to be converted to .csv once they are downloaded to the local server.


As shown in the code below, I am reading from a json file and deserializing it, and then initialize a timer that repeat every 5 seconds. Once the button is clicked the machine IP's address is pinged and the feedback time is recorded on a text file, at the same time, the file will be transferred to the PC using SFTP. (code is successfully working)


private void button1_Click(object sender, EventArgs e)

        {

            if (MessageBox.Show("Are you sure you want to Start", "STARTED", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)

            {

                //Read JSON file from the directory

                filePath = @"C:\temp\JSON\app-db.json";

                string text = File.ReadAllText(filePath);

                var currentList = JsonConvert.DeserializeObject<List<Datalogger>>(text);

                //Set the timer to 5 sec

                System.Windows.Forms.Timer Clock = new System.Windows.Forms.Timer

                {

                    Interval = 5000 //5 seconds

                };

                Clock.Start();


                Clock.Tick += new EventHandler(timer1_Tick);

            }

            else

            {

                this.Activate();

            }

           

        }

        private void timer1_Tick(object sender, EventArgs e)

        {

            filePath = @"C:\temp\JSON\app-db.json";

            string text = File.ReadAllText(filePath);

            var currentList = JsonConvert.DeserializeObject<List<Datalogger>>(text);

            foreach (var item in currentList)

            {

                List<String> hosts = new List<String>();

                for (Int32 i = 0; i < 10; ++i) hosts.Add(item.IPaddress);

                //Write IP time feedback to a txt file

                var average = hosts.AsParallel().WithDegreeOfParallelism(64).

                              Select(h => new Ping().Send(h).RoundtripTime).Average();

                //item.IPaddress = DateTime.Now.ToLongDateString() + "    " + DateTime.Now.ToLongTimeString();

                Console.WriteLine("IP:" + item.IPaddress + "," + "time=" + average + "ms");

                m_streamWriter.WriteLine("{0} {1} {2}",

                   DateTime.Now.ToLongTimeString(),

                   DateTime.Now.ToLongDateString(),

                   "IP:" + item.IPaddress + "," + "time=" + average + "ms");

                m_streamWriter.Flush();

                //Read JSON string values

                string host = item.IPaddress;

                string username = item.username;

                string password = item.password;

                string remoteDirectory = item.sourcefolder;

                string localDirectory = item.destfolder;

                string filextension = item.filextension;

                string removedownloaded = item.removedownloaded.ToString();

                //Connect to the SFTP server to downdload data

                using (SftpClient sftp = new SftpClient(host, username, password))

                {

                    try

                    {

                        sftp.Connect();


                        var files = sftp.ListDirectory(remoteDirectory);


                        foreach (var file in files)

                        {

                            try

                            {

                                string remoteFileName = file.Name;


                                if ((file.Name.EndsWith(filextension)) || (file.Name.EndsWith(filextension.ToLower())) || (file.Name.EndsWith(filextension.ToUpper())))

                                {

                                    using (Stream file1 = File.OpenWrite(Path.Combine(localDirectory, remoteFileName)))

                                    {


                                        string path = remoteDirectory + "/" + remoteFileName;

                                        sftp.DownloadFile(path, file1);

                                       


                                        if (removedownloaded == "1")

                                        {

                                            sftp.Delete(path);

                                        }

                                        else

                                        {

                                            sftp.DownloadFile(path, file1);

                                        }


                                    }

                                }


                            }

                            catch (Exception er1)

                            {

                                //MessageBox.Show("An exception has been caught " + er1.ToString());

                            }


                        }

                    }

                    catch (Exception entry)

                    {

                        MessageBox.Show(entry.Message);

                    }

                   

                }


            }

           

        }



and this is the JSON file.


[

  {

    "Record": 1,

    "IPaddress": "192.168.6.247",

    "Machinename": "taurus",

    "username": "root",

    "password": "root",

    "sourcefolder": "/home/root/conf",

    "destfolder": "C:/temp/Data1",

    "filextension": ".db",

    "removedownloaded": 0,

    "removecsv": 1,

    "removedb": 1

  }

]




I am trying to convert to upcoming file to .csv.


Any suggestions and thoughts on how to convert them?


I appreciate any help!


Continue reading...


Back
Top