goo blog サービス終了のお知らせ 

Visual Studio VB.NET Tips 集

プログラミング集です。
メインはVisual Basic .NET でございます。

DLLCopy

2012-06-10 10:19:35 | 日記

        /// <summary>
        /// 実行処理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            // ファイル名に「*」を含み、拡張子が「.dll」のファイルを最下層まで検索し取得する
            string[] strFilePathA = GetFilesMostDeep(@".\bin", "*.dll");
            string strFileNameA = string.Empty;

            // 取得したファイル名を列挙する
            foreach (string stFilePath in strFilePathA)
            {
                //DLL名取得
                strFileNameA = Path.GetFileName(stFilePath);
                //新DLLが存在したらコピー
                if (File.Exists(@".\dlldir\" + strFileNameA))
                {
                    //コピー実行
                    File.Copy(@".\dlldir\" + strFileNameA, stFilePath, true);
                    //.pbdも存在したらコピー
                    string strPBD = strFileNameA.Replace(".dll", ".pdb");
                    string strPBDpath = stFilePath.Replace(".dll", ".pdb");
                    if (File.Exists(@".\dlldir\" + strPBD))
                    {
                        //コピー実行
                        File.Copy(@".\dlldir\" + strPBD, strPBDpath, true);
                    }
                }
            }
        }

        /// ---------------------------------------------------------------------------------------
        /// <summary>
        ///     指定した検索パターンに一致するファイルを最下層まで検索しすべて返します。</summary>
        /// <param name="stRootPath">
        ///     検索を開始する最上層のディレクトリへのパス。</param>
        /// <param name="stPattern">
        ///     パス内のファイル名と対応させる検索文字列。</param>
        /// <returns>
        ///     検索パターンに一致したすべてのファイルパス。</returns>
        /// ---------------------------------------------------------------------------------------
        public static string[] GetFilesMostDeep(string stRootPath, string stPattern)
        {
            System.Collections.Specialized.StringCollection hStringCollection = (
                new System.Collections.Specialized.StringCollection()
            );

            // このディレクトリ内のすべてのファイルを検索する
            foreach (string stFilePath in System.IO.Directory.GetFiles(stRootPath, stPattern))
            {
                hStringCollection.Add(stFilePath);
            }

            // このディレクトリ内のすべてのサブディレクトリを検索する (再帰)
            foreach (string stDirPath in System.IO.Directory.GetDirectories(stRootPath))
            {
                string[] stFilePathes = GetFilesMostDeep(stDirPath, stPattern);

                // 条件に合致したファイルがあった場合は、ArrayList に加える
                if (stFilePathes != null)
                {
                    hStringCollection.AddRange(stFilePathes);
                }
            }

            // StringCollection を 1 次元の String 配列にして返す
            string[] stReturns = new string[hStringCollection.Count];
            hStringCollection.CopyTo(stReturns, 0);

            return stReturns;
        }
    }


最新の画像もっと見る