如何将文本文件中的值指定给visual studio c#?

如何将文本文件中的值指定给visual studio c#?,c#,wpf,visual-studio,C#,Wpf,Visual Studio,我正在使用visual studio 2015 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" x

我正在使用visual studio 2015 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>
我有一个文本文件,其中包含以下格式的浮点值:

 1.234
 145.765
 1.267
 145.957
第一个值是纬度,第二个值是经度。 这在第三和第四、第五和第六等重复进行

我想将文本文件中的第一个和第二个值分配给代码行

     pin.Location = new Location(1st_value,2nd_value);
然后它会在地图上添加一个图钉

但是我是一个新手,我不知道如何从文本文件中读取内容并为代码行添加值

如何将文本文件中的值分配给代码行

谢谢

这可能会帮助您: 使用
File.ReadAllLines
从文件中获取所有行(作为数组)。根据您的输入规范,
纬度
将位于第一行,
经度
将位于第二行,因此您可以通过它们的索引访问它们。使用
double.TryParse()。现在考虑下面的代码:

string textFilePath=@"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);
            }
        }

您可以使用
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));
}

一旦读取了文件内容,就可以在
列表中维护所有纬度和经度信息的集合,并且每个列表项都是一对纬度和经度值<代码>元组
应该可以解决这个问题

    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
        }
    }

一定要检查拐角处的情况,并进行相应的处理,例如,如果行不是2的乘法器,请键入每行文字等。

另一个奇怪的问题。:)谢谢你的回复:)我的代码正在运行。
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();
    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);