2016-06-13 116 views
2

我正在使用visual studio 2015 c#(對代碼進行一些修改)從網​​站tutorial創建另一個解決方案。如何將文本文件中的值分配給Visual Studio c#?

XAML文件:

<Window x:Class="WPFTestApplication.InsertPushpin" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:m="clr-namespace:Microsoft.Maps.MapControl.WPF;assembly=Microsoft.Maps.MapControl.WPF" 
    Width="1024" Height="768"> 

    <Grid x:Name="LayoutRoot" Background="White"> 
     <m:Map CredentialsProvider="INSERT_YOUR_BING_MAPS_KEY"> 
     </m:Map> 
    </Grid> 
    </Window> 

的xaml.cs文件如下:

using System; 
using System.Windows; 
using System.Windows.Controls; 
using System.Globalization; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using Microsoft.Maps.MapControl.WPF; 
using Microsoft.Maps.MapControl.WPF.Design; 

namespace WPFTestApplication 
{ 
public partial class AddPushpinToMap : Window 
{ 
    LocationConverter locConverter = new LocationConverter(); 

public AddPushpinToMap() 
{ 
    InitializeComponent(); 
    Pushpin pin = new Pushpin(); 
    pin.Location = new Location(37.1481402218342, -119.644248783588); 

    // Adds the pushpin to the map. 
    myMap.Children.Add(pin); 

    } 
} 
} 

我有一個包含這種格式的浮點值的文本文件:

1.234 
145.765 
1.267 
145.957 

第一個值是緯度,第二個值是經度。 這對於重複第3和第4,第5和第6等

我想從文本文件的第1和第2的值賦給代碼

 pin.Location = new Location(1st_value,2nd_value); 

線,然後將圖釘添加到地圖。

但我是一個新手,我不知道如何從文本文件中讀取並將值添加到該行代碼。

如何將文本文件中的值分配給代碼行?

感謝

+0

另一個奇怪的世界問題。 :) –

+0

感謝您的回覆:)我的代碼現在正在工作。 – Gajesh

回答

4

你可以使用File.ReadLines方法來讀取文件的內容。

作爲初學者,您可以使用foreach開始對列表進行迭代。

var lines = File.ReadLines(filepath).ToList(); 
var locations = new List<Location>(); 
if(lines.Count() %2 !=0) throw new ArgumentException("invalid no.of vertices"); 

for(int i=0;i<lines.Count();i+=2) 
{ 
    double lat = double.Parse(lines[i]); 
    double lon = double.Parse(lines[i+1]); 

    locations.Add(new Location(lat, lon)); 
} 

如果您熟悉Linq你可以用Linq如下做到這一點。

var locations = File.ReadLines(filepath) 
    .Select((line,i)=> new {line, index=i/2 }) 
    .GroupBy(x=>x.index) 
    .Select(x=> new Location(double.Parse(x.First().line),double.Parse(x.Last().line))) 
    .ToList(); 
1

這可能會幫助您: 使用File.ReadAllLines來從文件中的所有線路(如陣列)。根據您的輸入規格,latitude將位於第一行,longitude將位於第二行,以便您可以通過索引訪問它們。使用double.TryParse()方法將這些值轉換爲雙等值。現在考慮下面的代碼是:

string [email protected]"local path to the file"; 
var Lines= System.IO.File.ReadAllLines(textFilePath); 
double latitude,longitude; 
double.TryParse(Lines[0],out latitude); 
double.TryParse(Lines[1],out longitude); 
pin.Location = new Location(latitude,longitude); 
2

這應該給你的東西入手,

 using (StreamReader reader = new StreamReader("*** your filepath ***")) 
     { 
      while (!reader.EndOfStream) 
      { 
       double lat = double.Parse(reader.ReadLine()); 
       double lon = double.Parse(reader.ReadLine()); 

       pin.Location = new Location(lat, lon); 
      } 
     } 
1

一旦你讀取文件的內容,你可以保持所有的經度和緯度信息的收集List,每個表項是對緯度和經度值。 Tuple應該解決這裏的目的。

private void BuildGeoInfo() 
    { 
     string textFilePath = @"path to your text file"; 

     //Read all the contents of file as list of lines 
     var fileLines = System.IO.File.ReadAllLines(textFilePath).ToList(); 

     //This list will hold the Latitude and Longitude information in pairs 
     List<Tuple<double, double>> latLongInfoList = new List<Tuple<double, double>>(); 

     int index = 0; 
     while (index < fileLines.Count) 
     { 
      var latLongInfo = new Tuple<double, double>(
            Convert.ToDouble(fileLines[index]), 
            //++ to index to get value of next line 
            Convert.ToDouble(fileLines[index++])); 

      latLongInfoList.Add(latLongInfo); 

      index++; //++ to index to move to next line 
     } 
    } 

然後,您可以使用該資料收集這樣的例子 -

var latitude = latLongInfoList.First().Item1; 
var longitude = latLongInfoList.First().Item2; 
pin.Location = new Location(latitude,longitude); 

做檢查的極端情況,並相應地處理它們,就像如果什麼線都沒有在兩個乘數,每個文本行的類型等。

相關問題