如何在void中将数据保存到List,然后从另一个方法调用它

时间:2017-05-14 14:33:12

标签: c# list methods kml void

我正在研究一些计算车队费用的项目。所以基本上我需要读取所有二进制文件并将其保存到SQL Server中的表中。然后我需要在谷歌地球上展示一些车辆路线,这是我不知道如何解决它的地方。 所以我的问题是如何在这段代码中我可以将数据保存在列表中,然后将其调用到另一种方法。问题是我不能使用返回,所以我被卡住了。

 public static void WalkDirectoryTree(DirectoryInfo root)
        {
            FileInfo[] files = null;
            DirectoryInfo[] subDirs = null;

            // First, process all the files directly under this folder
            try
            {
                files = root.GetFiles("*.*");
            }
            // This is thrown if even one of the files requires permissions greater
            // than the application provides.
            catch (UnauthorizedAccessException e)
            {
                // This code just writes out the message and continues to recurse.
                // You may decide to do something different here. For example, you
                // can try to elevate your privileges and access the file again.
                Console.WriteLine(e.Message);
            }

            catch (DirectoryNotFoundException e)
            {
                Console.WriteLine(e.Message);
            }            
            if (files != null)
            {                
                foreach (FileInfo fi in files)
                {
                    // In this example, we only access the existing FileInfo object. If we
                    // want to open, delete or modify the file, then
                    // a try-catch block is required here to handle the case
                    // where the file has been deleted since the call to TraverseTree().
                    Console.WriteLine(fi.FullName);
                    List<HS1Data> listData = readHSdataFile(fi.FullName);                    
                    SqlCommand command;
                    string sql = null;
                    string sql2 = null;
                    foreach (HS1Data d in listData)
                    {

                        SqlDataReader dataReader;                        
                        try
                        {
                            sql = ("INSERT INTO dbo.Vozilo (matchX,matchY,matchSpeed,matchHeading,orgX,orgY,Speed,Heading)VALUES (" + Convert.ToString(d.matchX).Replace(",", ".") + "," +
                                Convert.ToString(d.matchY).Replace(",", ".") + "," +
                                d.matchSpeed + "," +
                                d.macthHeading + "," +
                                Convert.ToString(d.orgX).Replace(",", ".") + "," +
                                Convert.ToString(d.orgY).Replace(",", ".") + "," +
                                d.Speed + "," +
                                d.Heading + ");");
                            sql2 = "INSERT INTO dbo.GPS(UTC,filterPosition,localTime,TipPozicije,PositionTime)VALUES(" + d.UTC + "," +
                                d.filterPosition + ",'" + d.localTime.Year+"-"+d.localTime.Month+"-"+d.localTime.Day +"',"
                               + d.TipPozicije + "," + d.PositionTime+ ")";                          
                            command = new SqlCommand(sql, connection);
                            dataReader = command.ExecuteReader();
                            dataReader.Close();
                            command = new SqlCommand(sql2, connection);
                            dataReader = command.ExecuteReader();
                            dataReader.Close();


                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                        //save it to list??
                    }                 
                }
            }

            // Now find all the subdirectories under this directory.
            subDirs = root.GetDirectories();

            foreach (DirectoryInfo dirInfo in subDirs)
            {
                // Resursive call for each subdirectory.
                WalkDirectoryTree(dirInfo);
            }
        }

这是我尝试呼叫列表的地方,因此我可以使用经度和纬度来创建kml文件。

private void btnCreateKML_Click(object sender, EventArgs e)
        {
           //trying to call a list from WalkDirectoryTree

            // Ovdje je potrebno napisati odgovarajuću deklaraciju metod            
            {
                string izlaz = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
                izlaz += "<kml xmlns=\"http://www.opengis.net/kml/2.2\" xmlns:gx=\"http://www.google.com/kml/ext/2.2\" xmlns:kml=\"http://www.opengis.net/kml/2.2\" xmlns:atom=\"http://www.w3.org/2005/Atom\">\n";
                izlaz += "<Document>\n";

                //Potrebno je napisati odgovarajuću petlju koja iterira po iteratoru i (pogledaj sljedeći zadatak pa odluči koje vrijednosti su potrebne za i)
                for (int i = 0; i < listData.Count; i++)
                {
                    if (listData[i].UTC >= 1372636821 && listData[i].UTC <= 1372723045)
                    {
                        izlaz += "\t<Placemark>\n";
                        izlaz += "\t<name>" + Convert.ToString(i) + "</name>";
                        izlaz += "\t\t<LineString>\n";
                        izlaz += "\t\t\t<coordinates>\n";

                        /*
                        Potrebno je nadopuniti sljedeću liniju koda na svim mjestima na kojima piše XXX.
                        Ovaj dio koda odnosi se na crtanje linije određene početnom i završnom geografskom koordinatom. Prvi XXX potrebno 
                        je zamijeniti s geografskom dužinom prve točke, drugi XXX geografskom širinom prve točke, treći XXX geografskom
                        dužinom druge točke te četvrti XXX geografskom širinom druge točke.
                         * */
                        izlaz += "\t\t\t\t" + Convert.ToString(listData[i - 1].matchX).Replace(",", ".") + "," + Convert.ToString(listData[i - 1].matchY).Replace(",", ".") + ",0  " + Convert.ToString(listData[i].matchX).Replace(",", ".") + "," + Convert.ToString(listData[i].matchY).Replace(",", ".") + ",0\n";

                        izlaz += "\t\t\t</coordinates>\n";
                        izlaz += "\t\t</LineString>\n";


                        /*int r, g;
                        //Potrebno je napisati kod koji pohranjuje vrijednosti boja zelene i crvene u varijable r i g prema uputama zadatka


                        izlaz += "\t\t<Style>\n";
                        izlaz += "\t\t\t<LineStyle>\n";
                        izlaz += ("\t\t\t\t<color>#ff00" + g.ToString("X") + r.ToString("X") + "</color>\n");

                        izlaz += ("\t\t\t\t<width>5</width>\n");
                        izlaz += "\t\t\t</LineStyle>\n";
                        izlaz += "\t\t</Style>\n";

                        izlaz += "\t</Placemark>\n";*/
                    }                  
                }

                izlaz += "</Document>";
                izlaz += "</kml>";

                //Potrebno je napisati kod koji stvara datoteku Ruta.kml s relativnom putanjom te u nju upisuje vrijednost varijable izlaz.
                StreamWriter ispis = new StreamWriter("Ruta.kml");
            }

0 个答案:

没有答案