简体   繁体   English

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

[英]How to assign values from text file to visual studio c#?

I am creating another solution from a website tutorial using visual studio 2015 c# (with some modifications to the code). 我正在使用visual studio 2015 c#(对代码进行一些修改)从网​​站教程创建另一个解决方案。

The xaml file: 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>

The xaml.cs file is as follows: 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);

  }
 }
}

I have a text file which contains float values in this format: 我有一个文本文件,其中包含以下格式的浮点值:

 1.234
 145.765
 1.267
 145.957

The first value is the latitude and the 2nd value is the longitude. 第一个值是纬度,第二个值是经度。 This repeats for the 3rd and 4th, 5th and 6th etc. 这将重复第3和第4,第5和第6等。

I want to assign the 1st and 2nd values from the textfile to the line of code 我想将文本文件中的第1和第2个值分配给代码行

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

and then it will add a pushpin to the map. 然后它会在地图上添加图钉。

But I'm a newbie and I'm not sure how to read from the text file and add the value to that line of code. 但我是新手,我不知道如何从文本文件中读取并将值添加到该行代码中。

How can I assign the values from the text file to the line of code? 如何将文本文件中的值分配给代码行?

Thanks 谢谢

You could use File.ReadLines method to read the file contents. 您可以使用File.ReadLines方法来读取文件内容。

As a beginner you could start iterating over list using foreach . 作为初学者,您可以使用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));
}

If you are familiar with Linq you could do this with Linq as below. 如果您熟悉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();

This should give you something to start with, 这应该给你一些开始,

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

This may help you: Use File.ReadAllLines to get all Lines(as Array) from the File. 这可能对您有所帮助:使用File.ReadAllLines从文件中获取所有行(作为数组)。 As per your input specifications the latitude will be in the First Line and the longitude will be at the second line, so that you can access them through their index. 根据您的输入规格, latitude将位于第一行, longitude将位于第二行,以便您可以通过其索引访问它们。 use double.TryParse() method to convert those values into double equivalent. 使用double.TryParse()方法将这些值转换为double等价物。 Now consider the following code for this: 现在考虑以下代码:

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);

Once you read the file content, you could maintain collection of all the Latitude and Longitude information in List , and each list item would be pair of Latitude and Longitude values. 读取文件内容后,您可以维护List中所有纬度和经度信息的收集,每个列表项都是纬度和经度值对。 Tuple should solve the purpose here. 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
        }
    }

You can then use the data in collection like this for example - 然后,您可以使用此类集合中的数据,例如 -

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

Do check for the corner cases and handle them accordingly, like what if the lines are not in multiplier of two, type of each text line etc. 检查拐角情况并相应地处理它们,比如如果线不是两个乘数,每个文本行的类型等等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过从文本文件中读取字符将值分配给C#变量 - Assign values to C# variables by reading characters from a text file 如何使用Visual Studio在C#中创建文本文件? - how to create a text file in c# using visual studio? 引用文本文件Visual Studio C# - Referencing Text file Visual Studio C# 如何从文本文件 C# Visual Studio 解析特定数据 - How to parse for specific data from a text file C# visual studio 如何在Visual Studio / WPF / C#中创建“分配按钮” texbox? - How to create a “assign button” texbox in visual studio / wpf / c#? 从 C# 在 Visual Studio 中切换文件 - Switch file in Visual Studio from C# 有没有办法在 Visual Studio 中使用 C# 动态更改文本框的文本,以便该值是来自其他文本框的值的总和? - Is there a way to dynamically change the text of a textBox in Visual Studio with C# so that the value is a sum of values from other textboxes? 如何在Visual Studio 2008中使用C#将远程数据库值检索到文本框中 - How to retrieve Remote Database values into text box using C# in Visual Studio 2008 Visual C# - 从文本文件中读取并在包含不同值的两个数组中分隔值 - Visual C# - Reading from a text file and separating values in two arrays that hold different values 从文本文件读取后显示数据-C#,Visual Studio - Displaying Data after reading from text file - C#,Visual Studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM