0

内容が次のようにフォーマットされているテキストファイルを調べる方法を見つけようとしていました:

Date: 7/30/2013 12:00:00 AM 
Source Path: C:\FileMoveResults 
Destination Path: C:\Users\Documents\.NET Development\testing\ 
Last Folder Updated: 11.0.25.1

「最後に更新されたフォルダー」に応じて検索し、ソースパスと宛先パスを個別の変数に保存できる場所。

これを行うにはどうすればよいでしょうか?

ありがとう!

編集:

「foreachはboolをサポートしていません」というエラーが表示されるため、現在使用していないコードは次のとおりです。

using (var reader = new StreamReader(GlobalVars.strLogPath))
{
foreach (var items in File.ReadLines(GlobalVars.strLogPath))
{
foreach(var item in items.StartsWith("Destination Path: "))
{
//nothing here yet
4

4 に答える 4

0

こんなことを考えていました。もう少し複雑ですが、ファイルの形式が変更された場合でも、ループして各セットの値を見つけることができるようにするのに役立ちます。

    public void ReadFile(string filepath)
    {
        Dictionary<string, string> mypaths = new Dictionary<string, string>();
        string line;
        string line1 = null;
        string line2 = null;
        bool store = false;
        using(var rdr = new StreamReader(filepath))
        {
            while((line = rdr.ReadLine()) != null)
            {
                if(store = false && line.ToLower().Contains("source path:"))
                {
                    line1 = line;
                    store = true;
                }
                else if (store = true && line.ToLower().Contains("destination path:"))
                {
                    line2 = line;
                }
                else if (line.ToLower().Contains("last folder updated:"))
                {
                    var myvaluetoinspect = line;
                    if(1==1) // my condition
                    {
                        mypaths.Add(line1, line2);
                    }
                    // Clear and start over
                    store = false;
                    line1 = null;
                    line2 = null;
                }
                else
                {
                    store = false;
                    line1 = null;
                    line2 = null;
                }
            }
        }
    }
于 2013-07-30T20:34:57.910 に答える