0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Visual Studio | WPF > Resource > csvファイルで定義した数値を読込む (//でのコメントアウト対応)

Last updated at Posted at 2017-12-25
動作環境
Windows 8.1 Pro (64bit)
Microsoft Visual Studio 2017 Community

処理内容

上記と似たようなことをC#で行いたい。

includeでは無理そうなので、リソースとして読込むことで対応する。

データファイル

pi_data.csv
3, 1, 4, 1, 5,
//9, 2, 6, 5, 3,
5, 8, 9, 7, 9,

このファイルをリソースとしてプロジェクトに追加しておく。

@gushwell さんの下記を参考にします。
WPFサンプル:リソースファイルを扱う

code

MainWindow.xaml
<Window x:Class="_171225_t1624_includeCsv.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:_171225_t1624_includeCsv"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Button Name="B_check" Content="check" Height="30"
                Click="B_check_Click"/>
    </Grid>
</Window>
MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
// 以下を追加した
using rgexp = System.Text.RegularExpressions;

namespace _171225_t1624_includeCsv
{
    /// <summary>
    /// MainWindow.xaml の相互作用ロジック
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private string[] readCsvResources(string filename)
        {
            var fileUri = new Uri(filename, UriKind.Relative);
            var info = Application.GetResourceStream(fileUri);
            var work = new System.IO.StreamReader(info.Stream).ReadToEnd();

            // "//で始まる行の削除"
            var lines = work.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
            string res = "";
            foreach(var elem in lines)
            {
                res += rgexp.Regex.Replace(elem, "^//(.*)$", "", rgexp.RegexOptions.Multiline);
            }

            // 空白の削除
            res = res.Replace(" ", "");

            return res.Split(',');
        }

        private void B_check_Click(object sender, RoutedEventArgs e)
        {
            var alist = readCsvResources("pi_data.csv");
            //var alist = readCsvResources("napier_data.csv");

            //
            string msg = string.Join(" ", alist);
            MessageBox.Show(msg);
        }
    }
}

実行

qiita.png

下記の二行目はきちんと除外されている。

3, 1, 4, 1, 5,
//9, 2, 6, 5, 3,
5, 8, 9, 7, 9,

link

unity > 外部ファイルで定数を定義したものを使う > include "xxx.h"の代わり
を使うよりは本記事の方法の方がソフトのデバッグに使いやすいように思う。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?