簡體   English   中英

如何將文本文件中的值賦給visual studio c#?

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

我正在使用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>

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

這應該給你一些開始,

        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.ReadAllLines從文件中獲取所有行(作為數組)。 根據您的輸入規格, latitude將位於第一行, longitude將位於第二行,以便您可以通過其索引訪問它們。 使用double.TryParse()方法將這些值轉換為double等價物。 現在考慮以下代碼:

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

讀取文件內容后,您可以維護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);

檢查拐角情況並相應地處理它們,比如如果線不是兩個乘數,每個文本行的類型等等。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM