如何将文本文件中的值分配给Visual Studio c#?
问题描述:
我正在使用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);
线,然后将图钉添加到地图。
但我是一个新手,我不知道如何从文本文件中读取并将值添加到该行代码。
如何将文本文件中的值分配给代码行?
感谢
答
你可以使用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();
答
这可能会帮助您: 使用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);
答
这应该给你的东西入手,
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);
}
}
答
一旦你读取文件的内容,你可以保持所有的经度和纬度信息的收集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);
做检查的极端情况,并相应地处理它们,就像如果什么线都没有在两个乘数,每个文本行的类型等。
另一个奇怪的世界问题。 :) –
感谢您的回复:)我的代码现在正在工作。 – Gajesh