/*public string StageRequired(string ApproachNode, string NextLinkNumber) * { * string Returner = ""; * using (StreamReader ReadFile = new StreamReader(PrioritiesStages)) * { * string FileLine; * while ((FileLine = ReadFile.ReadLine()) != null) * { * if (FileLine.Contains("link " + ApproachNode + " to " + NextLinkNumber + ": ")) * { * string[] SplitString = FileLine.Split(' '); * Returner = SplitString[4]; * return Returner; * } * } * } * return "null"; * }*/ /// <summary> /// This returns the direction they are turning. (i.e. 'Left'.) /// </summary> /// <returns></returns> public string NextTurnDirection(string ApproachNode, string JunctionNode, string TurningNumber) { string Returner = ""; string WithoutCommaReturner = ""; using (StreamReader ReadFile = new StreamReader(LinkTurningFile)) { string FileLine; while ((FileLine = ReadFile.ReadLine()) != null) { if (FileLine.Contains("Link " + ApproachNode + " " + JunctionNode + ": ")) { string[] SplitString = FileLine.Split(' '); if (SplitString[4] == "None,") { return("None"); } if (SplitString[5] == "Left," || SplitString[5] == "Straight," || SplitString[5] == "Right,") { Returner = SplitString[5 + Convert.ToInt16(TurningNumber)]; WithoutCommaReturner = Returner.Substring(0, Returner.Length - 1); return(WithoutCommaReturner); } else { Returner = SplitString[6 + Convert.ToInt16(TurningNumber)]; WithoutCommaReturner = Returner.Substring(0, Returner.Length - 1); return(WithoutCommaReturner); } } } } return("null"); }
//Loads in all the data from the master item list text file, organize items from the FullItemList into the FullItemDictionary by combining the ItemData with what was read from the text file private void Awake() { //Assign our singleton class instance Instance = this; //Import the master item list file listening every item and its ItemNumber values string[] FileLines = System.IO.File.ReadAllLines(Application.dataPath + "/Exports/MasterItemList.txt"); Log.PrintChatMessage("Reading " + FileLines.Length + " lines in the MasterItemList file"); //string[] FileLines = System.IO.File.ReadAllLines("C:/Users/Harley/Desktop/mmo-client/Assets/Exports/MasterItemList.txt"); //Look through and process each line of the text file foreach (string FileLine in FileLines) { //Split apart each line of the file to seperate the items information apart string[] LineSplit = FileLine.Split(':'); //Each line should split into 5 parts, being the Name, DisplayName, Type, Slot and ItemNumber values string ItemName = LineSplit[0]; string ItemDisplayName = LineSplit[1]; ItemType ItemType = FindItemType(LineSplit[2]); EquipmentSlot ItemSlot = FindItemSlot(LineSplit[3]); int ItemNumber = Int32.Parse(LineSplit[4]); //Fetch the relevant items ItemData object from the FullItemList array, using the ItemName to find it ItemData ItemData = GetItemData(ItemName); //Update the items ItemNumber value, then store it into the dictionary with the ItemNumber as its key ItemData.ItemNumber = ItemNumber; FullItemDictionary.Add(ItemNumber, ItemData); } }
public string LinkTurningFile = @"C:\Documents and Settings\Siemens\Desktop\Andrew's Work\Paramics Models\Sopers\SopersLaneApproachMovements.txt"; //This is the clockwise link order //public string LinkFile = @"C:\Documents and Settings\Siemens\Desktop\Andrew's Work\Paramics Models\Poole Junction\Paramics 2010.1 Version\Cabot Lane Poole V3\links"; //public string PrioritiesFile = @"C:\Documents and Settings\Siemens\Desktop\Andrew's Work\Paramics Models\Poole Junction\Paramics 2010.1 Version\Cabot Lane Poole V3\priorities"; //public string PrioritiesStages = @"C:\Documents and Settings\Siemens\Desktop\Andrew's Work\Paramics Models\Simple Crossroads\Simple Crossroads\priorities_file"; //This is the required stage /// <summary> /// This returns the link number being turned onto. (i.e. moving from link 1 0 to link 0 3 /// would return '3'. /// </summary> /// <returns></returns> public string NextLinkNumber(string ApproachNode, string JunctionNode, string TurningNumber) { string Returner = ""; string WithoutCommaReturner = ""; using (StreamReader ReadFile = new StreamReader(LinkTurningFile)) { string FileLine; while ((FileLine = ReadFile.ReadLine()) != null) { if (FileLine.Contains("Link " + ApproachNode + " " + JunctionNode + ": ")) { string[] SplitString = FileLine.Split(' '); Returner = SplitString[3 + Convert.ToInt16(TurningNumber)]; WithoutCommaReturner = Returner.Substring(0, Returner.Length - 1); return(WithoutCommaReturner); //break; } } } /*using (StreamReader ReadFile = new StreamReader(LinkFile)) * { * string FileLine2; * while ((FileLine2 = ReadFile.ReadLine()) != null) * { * if (FileLine2.Contains("link " + JunctionNode + " " + WithoutCommaReturner)) * { * return WithoutCommaReturner; * } * } * }*/ return("null"); }
public EditLink(String NetPath) : base(NetPath) { FileName = "links"; using (StreamReader ReadFile = new StreamReader((NetPath + "\\" + FileName)))// open the paths file { try { string FileLine; while ((FileLine = ReadFile.ReadLine()) != null)//read the file line by line { if (FileLine.Contains("link ") && FileLine.Contains("category")) { string[] splitline = FileLine.Split(new Char[] { ' ' });// split the line up into an array of strings string StartNode = splitline[1]; string EndNode = splitline[2]; int Cat = Convert.ToInt32(splitline[4]); LinkList.Add(new N_W_Link(StartNode, EndNode, Cat, false));//TODO un hardcode false to read oneway data from file } } } catch (Exception e) { // Let the user know what went wrong. Console.WriteLine("The pathroutes file could not be read:"); Console.WriteLine(e.Message); } } }
//*Constructors public EditNodes(string NetPath) : base(NetPath) { FileName = "nodes"; using (StreamReader ReadFile = new StreamReader((NetPath + "\\" + FileName)))// open the paths file { try { string FileLine; N_W_Node temp = new N_W_Node(); while ((FileLine = ReadFile.ReadLine()) != null)//read the file line by line { if (FileLine.Contains("node ")) { string[] splitline = FileLine.Split(new Char[] { ' ' }); // split the line up into an array of strings string NodeNum = splitline[1]; string[] splitline2 = FileLine.Split(new Char[] { 't', 'm', ',' }); // split the line up into an array of strings double X = Convert.ToDouble(splitline2[1]); double Y = Convert.ToDouble(splitline2[3]); double Z = Convert.ToDouble(splitline2[5]); //temp = new N_W_Node(NodeNum,X,Y,Z); NodeList.Add(new N_W_Node(NodeNum, X, Y, Z)); } } } catch (Exception e) { // Let the user know what went wrong. Console.WriteLine("The pathroutes file could not be read:"); Console.WriteLine(e.Message); } } }
//*Constructor public EditPaths(string NetPath) : base(NetPath) { FileName = "paths"; using (StreamReader ReadFile = new StreamReader((NetPath + "\\" + FileName)))// open the paths file { try { string FileLine; N_W_Path temp = new N_W_Path(); LinkID tLiD = new LinkID(); while ((FileLine = ReadFile.ReadLine()) != null)//read the file line by line { if (FileLine.Contains("number ")) { temp = new N_W_Path(); string[] splitline = FileLine.Split(new Char[] { ' ' });// split the line up into an array of strings //read the data from the file into the class object lists int index = splitline.Length; index--; int num = Convert.ToInt32(splitline[index]); temp.PathIDn = num; } if (FileLine.Contains("name ")) { string[] splitline = FileLine.Split(new Char[] { '"' });// split the line up into an array of strings temp.PathName = splitline[1]; } if (FileLine.Contains("link ")) { string[] splitline = FileLine.Split(new Char[] { ':' });// split the line up into an array of strings string[] splitsplitline1 = splitline[0].Split(new Char[] { ' ' }); string[] splitsplitline2 = splitline[1].Split(new Char[] { ' ' }); int index1 = splitsplitline1.Length; int index2 = 0; index1--; string num1 = splitsplitline1[index1]; string num2 = splitsplitline2[index2]; tLiD = new LinkID(num1, num2); temp.PathDescription.Add(tLiD); } if (FileLine.Contains(" end")) { PathList.Add(temp); } } } catch (Exception e) { // Let the user know what went wrong. Console.WriteLine("The pathroutes file could not be read:"); Console.WriteLine(e.Message); } } }
//*Constructor public EditZones(string NetPath) : base(NetPath) { FileName = "zones"; using (StreamReader ReadFile = new StreamReader((NetPath + "\\" + FileName)))// open the paths file { try { string FileLine; N_W_Zone temp = new N_W_Zone(); while ((FileLine = ReadFile.ReadLine()) != null)//read the file line by line { if (FileLine.Contains("zone ") && !FileLine.Contains("Count")) { string[] splitline = FileLine.Split(new Char[] { ' ' });// split the line up into an array of strings temp.ZoneNum = Convert.ToInt32(splitline[1]); } else if (FileLine.Contains("m ") && !FileLine.Contains("max") && !FileLine.Contains("min") && !FileLine.Contains("centroid")) { string[] splitline = FileLine.Split(new Char[] { ' ' });// split the line up into an array of strings double[] corner = new double[2] { Convert.ToDouble(splitline[0]), Convert.ToDouble(splitline[2]) }; temp.Corners.Add(corner); } else if (FileLine.Contains("max")) { string[] splitline = FileLine.Split(new Char[] { ' ' });// split the line up into an array of strings temp.Max = new double[2] { Convert.ToDouble(splitline[1]), Convert.ToDouble(splitline[3]) }; } else if (FileLine.Contains("min")) { string[] splitline = FileLine.Split(new Char[] { ' ' });// split the line up into an array of strings temp.Min = new double[2] { Convert.ToDouble(splitline[1]), Convert.ToDouble(splitline[3]) }; } else if (FileLine.Contains("centroid")) { ZoneList.Add(temp); temp = new N_W_Zone(); } } } catch (Exception e) { // Let the user know what went wrong. Console.WriteLine("The pathroutes file could not be read:"); Console.WriteLine(e.Message); } } }
//class constructor public ReadPointLoopFiles(string NetworkName, int ModelTimeofDay, int dT) : base(NetworkName) { string[] Paths = Directory.GetFiles(LogDir, "point-*.csv"); TimeSpan TS = new TimeSpan(0, 0, ModelTimeofDay / 100); string TimeOfDay = TS.ToString(); foreach (string p in Paths) { FileInfo f = new FileInfo(p); DateTime StartTime = new DateTime(); DateTime EndTime = new DateTime(); StartTime = StartTime.AddSeconds(ModelTimeofDay / 100 - dT); EndTime = EndTime.AddSeconds(ModelTimeofDay / 100); LoopRecord LoopData = new LoopRecord(StartTime, EndTime, f.Name); using (StreamReader ReadFile = new StreamReader(File.Open(p, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))) { try { string FileLine; while ((FileLine = ReadFile.ReadLine()) != null) { if (!FileLine.Contains("Day")) { string[] Fields = FileLine.Split(','); DateTime Detection = new DateTime(); DateTime Holder = Convert.ToDateTime(Fields[1]); Detection = Detection.Add(Holder.TimeOfDay); int Comparitor1 = Detection.CompareTo(StartTime); int Comparitor2 = Detection.CompareTo(EndTime); if (Comparitor1 == 1) { if (Comparitor2 <= 0) { LoopData.AddData(Detection, Convert.ToDouble(Fields[8])); } } } } } catch (Exception e) { // Let the user know what went wrong. Console.WriteLine("A point loop file could not be read:"); Console.WriteLine(e.Message); } } LoopSensors.Add(LoopData); } }
//Method for saving the drawing private void openToolStripMenuItem_Click(object sender, EventArgs e) { string fileName; DialogResult Result; StreamReader fileReader; using (OpenFileDialog fileChooser = new OpenFileDialog()) { Result = fileChooser.ShowDialog(); fileName = fileChooser.FileName; } if (fileName == string.Empty) { MessageBox.Show("You must have a name for the file", "Please insert File Name", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { try { points = new List <Circle>(); FileStream input = new FileStream(fileName, FileMode.Open, FileAccess.Read); fileReader = new StreamReader(input); string FileLine; do { FileLine = fileReader.ReadLine(); if (FileLine != null) { string[] parts = FileLine.Split(','); Circle temporary = new Circle(Convert.ToInt32(parts[0]), Convert.ToInt32(parts[1]), Convert.ToInt32(parts[2]), Color.FromName(parts[3])); points.Add(temporary); } }while (FileLine != null); panel1.Invalidate(); input.Close(); fileReader.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message, "You chose the wrong file. ", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }
private void dataGrid1_Drop_csv(string file) { List <string> FileLines = new List <string>(); FileLines = File.ReadAllLines(file).ToList(); List <CSVDragObject> CSVDOList = new List <CSVDragObject>(); foreach (string FileLine in FileLines) { string[] ColumnsInLine = FileLine.Split(",".ToCharArray()); CSVDOList.Add(new CSVDragObject(ColumnsInLine[0], ColumnsInLine[1])); } dg1.ItemsSource = null; dg1.ItemsSource = CSVDOList; }
//costructor function private void ConstructorFunction() { using (StreamReader ReadFile = new StreamReader((LogDir + @"\vehicleroutes.csv"))) { try { string FileLine; while ((FileLine = ReadFile.ReadLine()) != null) { if (!FileLine.Contains("Vehicle")) { string[] splitstring = FileLine.Split(new char[] { ',' }); FileData TempDat = new FileData(); TempDat.Tag = Convert.ToInt32(splitstring[0]); TempDat.Vtype = Convert.ToInt32(splitstring[3]); TempDat.Origin = Convert.ToInt32(splitstring[1]); TempDat.Destination = Convert.ToInt32(splitstring[2]); TempDat.NextLink = "NULL2"; //have made it 512 as opposed to 511 to help notice a different problem if it arises TempDat.NextNextLink = "NULL2"; TempDat.NextTurn = "NULL2"; TempDat.NextNextTurn = "NULL2"; string[] splitsplitstring = splitstring[7].Split(new char[] { '"', ':' }); string LinkStart = splitsplitstring[1]; string LinkEnd = splitsplitstring[2]; TempDat.EnterLink = new LinkID(LinkStart, LinkEnd); TempDat.EnterTime = DateTime.Parse(splitstring[8]);//change this to include date if relevant Fdata.Add(TempDat); } } } catch (Exception e) { // Let the user know what went wrong. Console.WriteLine("The events file could not be read:"); Console.WriteLine(e.Message); } } }
//constructor function private void ConstructorFunction() { using (StreamReader ReadFile = new StreamReader((LogDir + @"\events.csv"))) { try { string FileLine; while ((FileLine = ReadFile.ReadLine()) != null) { if (!FileLine.Contains("Day")) { string[] splitstring = FileLine.Split(new char[] { ',' }); if ((Convert.ToInt32(splitstring[3])) == 23) { FileData TempDat = new FileData(); TempDat.Tag = Convert.ToInt32(splitstring[9]); TempDat.Vtype = Convert.ToInt32(splitstring[4]); TempDat.Lane = Convert.ToInt32(splitstring[8]); TempDat.Vspeed = (Convert.ToDouble(splitstring[5])) * 0.44704;//convert speed to m/s TempDat.LinkDist = Convert.ToDouble(splitstring[6]); string[] splitsplitstring = splitstring[2].Split(new char[] { '"', ':' }); string LinkStart = splitsplitstring[1]; string LinkEnd = splitsplitstring[2]; TempDat.OnLink = new LinkID(LinkStart, LinkEnd); TempDat.AtTime = DateTime.Parse(splitstring[1]);//change this to include date if relevant Fdata.Add(TempDat); } } } } catch (Exception e) { // Let the user know what went wrong. Console.WriteLine("The events file could not be read:"); Console.WriteLine(e.Message); } } }
//*Constructor public EditDetectors(string NetPath) : base(NetPath) { FileName = "detectors"; using (StreamReader ReadFile = new StreamReader((NetPath + "\\" + FileName)))// open the paths file { try { string FileLine; Detector temp = new Detector(); LinkID tLiD = new LinkID(); while ((FileLine = ReadFile.ReadLine()) != null)//read the file line by line { if (!FileLine.Contains("Detector Count")) { string[] splitline = FileLine.Split(new Char[] { '"' });// split the line up into an array of strings temp.Name = splitline[1]; string[] splitline2 = FileLine.Split(new Char[] { ' ' });// split the line up into an array of strings temp.Type = splitline2[0]; temp.LinkDist = Convert.ToDouble(splitline2[5]); tLiD = new LinkID(splitline2[9]); temp.OnLink = tLiD; temp.Length = Convert.ToDouble(splitline2[12]); DetectorList.Add(temp); temp = new Detector(); } } } catch (Exception e) { // Let the user know what went wrong. Console.WriteLine("The detectors file could not be read:"); Console.WriteLine(e.Message); } } }
//**Constructor public Editpathroutes(string NetPath) : base(NetPath) { FileName = "pathroutes"; using (StreamReader ReadFile = new StreamReader(String.Concat(NetPath, "\\", FileName)))// open the pathroutes file { try { string FileLine; while ((FileLine = ReadFile.ReadLine()) != null)//read the file line by line { if (FileLine.Contains("Path ")) { string[] splitline = FileLine.Split(new Char[] { ' ', '"' });// split the line up into an array of strings //read the data from the file into the class object lists RouteNames.Add(splitline[3]); Profile.Add(Convert.ToInt32(splitline[6])); Matrix.Add(Convert.ToInt32(splitline[8])); Rate.Add(Convert.ToInt32(splitline[10])); } } } catch (Exception e) { // Let the user know what went wrong. Console.WriteLine("The pathroutes file could not be read:"); Console.WriteLine(e.Message); } } //Check to make sure that all the lists are the same length if (RouteNames.Count != Profile.Count || RouteNames.Count != Matrix.Count || RouteNames.Count != Rate.Count) { Console.WriteLine("Warning: The lists of data in PathRoutes are not all the same length"); } }
//constructor function private void ConstructorFunction() { try { DirectoryInfo LogDirI = new DirectoryInfo(LogDir); FileInfo[] snaplist = LogDirI.GetFiles("snap*"); DateTime TimeNow = new DateTime(); DateTime ZeroHour = DateTime.Parse("00:00:00"); LinkID LinkNow = new LinkID(); ReadTurningIntention RTI = new ReadTurningIntention(); string TempVariableNextLink = ""; string TempVariableNextNextLink = ""; string TempVariableNextTurn = ""; string TempVariableNextNextTurn = ""; FileInfo snapfile = snaplist[snaplist.Length - 1]; using (StreamReader ReadFile = new StreamReader(snapfile.FullName)) { string FileLine; while ((FileLine = ReadFile.ReadLine()) != null) { if (FileLine.Contains("snapshot at time")) { string[] splitline = FileLine.Split(new char[] { ' ' }); double TimeSecs = Convert.ToDouble(splitline[3]); TimeNow = ZeroHour.AddSeconds(TimeSecs); } else if (FileLine.Contains("on link")) { string[] splitline = FileLine.Split(new char[] { ' ', ':' }); LinkNow = new LinkID(splitline[2], splitline[3]); //ApproachNode = splitline[2]; //JunctionNode = splitline[3]; } else if (FileLine.Contains("type ")) { FileData TempDat = new FileData(); if (FileLine.Contains("path")) { string[] splitline = FileLine.Split(new char[] { ' ', '"' }); TempDat.Vtype = Convert.ToInt32(splitline[1]) + 1; TempDat.Origin = Convert.ToInt32(splitline[15]) + 1; TempDat.Destination = Convert.ToInt32(splitline[16]) + 1; TempDat.Vspeed = Convert.ToDouble(splitline[13]); TempDat.LinkDist = Convert.ToDouble(splitline[8]); TempDat.RouteName = splitline[6]; TempDat.OnLink = LinkNow; TempDat.AtTime = TimeNow; double bornsecs = Convert.ToDouble(splitline[17]); TempDat.BornTime = ZeroHour.AddSeconds(bornsecs); TempDat.MagicNumbers = ("[" + splitline[10]); TempDat.MagicNumbers += ("," + splitline[20] + "]"); //This needs to be checked Fdata.Add(TempDat); } else { string[] splitline = FileLine.Split(new char[] { ' ' }); TempDat.Vtype = (Convert.ToInt32(splitline[1])) + 1; TempDat.Origin = (Convert.ToInt32(splitline[10])) + 1; TempDat.Destination = (Convert.ToInt32(splitline[11])) + 1; TempDat.Lane = (Convert.ToInt32(splitline[7])); TempDat.Vspeed = Convert.ToDouble(splitline[8]); TempDat.LinkDist = Convert.ToDouble(splitline[3]); TempDat.OnLink = LinkNow; TempDat.AtTime = TimeNow; double bornsecs = Convert.ToDouble(splitline[12]); TempDat.BornTime = ZeroHour.AddSeconds(bornsecs); TempDat.MagicNumbers = ("[" + splitline[5]); TempDat.MagicNumbers += ("," + splitline[15] + "]"); TempVariableNextLink = RTI.NextLinkNumber(LinkNow.StartNode, LinkNow.EndNode, splitline[13]); if (TempVariableNextLink == "null") { TempDat.NextLink = "99999"; //TODO 99999 could be a next link number! TempDat.NextNextLink = "99999"; } else { TempDat.NextLink = TempVariableNextLink; } TempVariableNextNextLink = RTI.NextLinkNumber(LinkNow.EndNode, TempVariableNextLink, splitline[14]); if (TempVariableNextNextLink == "null") { TempDat.NextNextLink = "99999"; } else { TempDat.NextNextLink = TempVariableNextNextLink; } TempVariableNextTurn = RTI.NextTurnDirection(LinkNow.StartNode, LinkNow.EndNode, splitline[13]); if (TempVariableNextTurn == "null") { TempDat.NextTurn = "None"; //99999 shows an error TempDat.NextNextTurn = "None"; } else { TempDat.NextTurn = TempVariableNextTurn; } TempVariableNextNextTurn = RTI.NextTurnDirection(LinkNow.EndNode, TempVariableNextLink, splitline[14]); if (TempVariableNextNextTurn == "null") { TempDat.NextNextTurn = "None"; //99999 shows an error } else { TempDat.NextNextTurn = TempVariableNextNextTurn; } //TempDat.NextTurn = "Null10"; //TempDat.NextNextTurn = "Null10"; Fdata.Add(TempDat); } } } } } catch (Exception e) { // Let the user know what went wrong. Console.WriteLine("Error reading snapfiles:"); Console.WriteLine(e.Message); } }
//constructor function private void ConstructorFunction() { try { DirectoryInfo LogDirI = new DirectoryInfo(LogDir); FileInfo[] snaplist = LogDirI.GetFiles("snap*"); DateTime TimeNow = new DateTime(); DateTime ZeroHour = DateTime.Parse("00:00:00"); LinkID LinkNow = new LinkID(); foreach (FileInfo snapfile in snaplist) { using (StreamReader ReadFile = new StreamReader(snapfile.FullName)) { string FileLine; while ((FileLine = ReadFile.ReadLine()) != null) { if (FileLine.Contains("snapshot at time")) { string[] splitline = FileLine.Split(new char[] { ' ' }); double TimeSecs = Convert.ToDouble(splitline[3]); TimeNow = ZeroHour.AddSeconds(TimeSecs); } else if (FileLine.Contains("on link")) { string[] splitline = FileLine.Split(new char[] { ' ', ':' }); LinkNow = new LinkID(splitline[2], splitline[3]); } else if (FileLine.Contains("type ")) { FileData TempDat = new FileData(); if (FileLine.Contains("path")) { string[] splitline = FileLine.Split(new char[] { ' ', '"' }); TempDat.Vtype = Convert.ToInt32(splitline[1]) + 1; TempDat.Origin = Convert.ToInt32(splitline[15]) + 1; TempDat.Destination = Convert.ToInt32(splitline[16]) + 1; TempDat.Vspeed = Convert.ToDouble(splitline[13]); TempDat.LinkDist = Convert.ToDouble(splitline[8]); TempDat.RouteName = splitline[6]; TempDat.OnLink = LinkNow; TempDat.AtTime = TimeNow; double bornsecs = Convert.ToDouble(splitline[17]); TempDat.BornTime = ZeroHour.AddSeconds(bornsecs); TempDat.MagicNumbers = ("[" + splitline[10]); TempDat.MagicNumbers += ("," + splitline[20] + "]");//This needs to be checked Fdata.Add(TempDat); } else { string[] splitline = FileLine.Split(new char[] { ' ' }); TempDat.Vtype = (Convert.ToInt32(splitline[1])) + 1; TempDat.Origin = (Convert.ToInt32(splitline[10])) + 1; TempDat.Destination = (Convert.ToInt32(splitline[11])) + 1; TempDat.Vspeed = Convert.ToDouble(splitline[8]); TempDat.LinkDist = Convert.ToDouble(splitline[3]); TempDat.NextLink = splitline[13]; TempDat.NextNextLink = splitline[14]; TempDat.NextTurn = "NULL3"; TempDat.NextNextTurn = "NULL3"; TempDat.OnLink = LinkNow; TempDat.AtTime = TimeNow; double bornsecs = Convert.ToDouble(splitline[12]); TempDat.BornTime = ZeroHour.AddSeconds(bornsecs); TempDat.MagicNumbers = ("[" + splitline[5]); TempDat.MagicNumbers += ("," + splitline[15] + "]"); Fdata.Add(TempDat); } } } } } } catch (Exception e) { // Let the user know what went wrong. Console.WriteLine("Error reading snapfiles:"); Console.WriteLine(e.Message); } }
/// <summary> /// 把文件里的数据读入数据库 /// </summary> private void ToDB() { delegation d_a = new delegation(GApp_Add); long FileTime = 0; string[] FileArr = System.IO.Directory.GetFiles(Path); while (FileArr.Length > 0) { //对FileArr 重新赋值 FileArr = System.IO.Directory.GetFiles(Path); //给一次机会 if (FileArr.Length == 0) { Thread.Sleep(120000); continue; } Array.Sort(FileArr); //如果第一个文件的创建时间小于两分钟,那么有可能还没写好,等待两分钟 FileInfo fi = new FileInfo(FileArr[0]); if (DateTime.Now.Subtract(fi.CreationTime).TotalSeconds < 120) { Thread.Sleep(120000); } //如果时间小于10秒 if (fi.CreationTime.Ticks - FileTime < 100000000) { FileTime = fi.CreationTime.Ticks; } else { FileTime = fi.CreationTime.Ticks; //用 ; 对股票进行拆分 StreamReader sr = new StreamReader(FileArr[0]); string[] FileContent = sr.ReadToEnd().Split(';'); string com = "INSERT INTO stock.0000 (code,date,time,open,close) values"; foreach (string FileLine in FileContent) { //用 , 对股票字段进行拆分 string[] StockContent = FileLine.Split(','); if (StockContent.Length == 33) { string code = StockContent[0].Substring(StockContent[0].LastIndexOf("_") + 1, 8); string date = StockContent[30]; string time = StockContent[31]; string open = StockContent[2]; string close = StockContent[3]; com = com + "('" + code + "','" + date + "','" + time + "','" + open + "','" + close + "'),"; } } com = com.Substring(0, com.Length - 1); ms.I_D_U(com); sr.Close(); } File.Delete(FileArr[0]); string filename_p = FileArr[0].Substring(FileArr[0].LastIndexOf('\\') + 1, FileArr[0].Length - FileArr[0].LastIndexOf('\\') - 1); lb_Now.Invoke(d_a, filename_p + "读取成功。"); //对FileArr 重新赋值 FileArr = System.IO.Directory.GetFiles(Path); } lb_Now.Invoke(d_a, L + "入库完成。"); }