UserControls/AttachmentFolderSelectionControl.cs

using System;
using System.IO;
using System.Drawing;
using System.Windows.Forms;
using ChatworkBulkSender.Utils;

namespace ChatworkBulkSender.UserControls
{
    public partial class AttachmentFolderSelectionControl : AbstractUserControl
    {
        private Constants.SEND_TYPE _sendType;
        public AttachmentFolderSelectionControl()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 「定期送信」「不定期送信」に応じて表示レイアウトを調整する
        /// </summary>
        /// <param name="sendType"></param>
        public void ChangeMode(Constants.SEND_TYPE sendType)
        {
            _sendType = sendType;
            if (_sendType == Constants.SEND_TYPE.ADHOC_SENDING)
            {
                lblTitle.Text = "添付ファイル(任意)";
            }
        }

        public string GetPath()
        {
            return txtAttachmentPath.Text;
        }

        public void SetPath(string path)
        {
            txtAttachmentPath.Text = path;
        }

        /// <summary>
        /// パス指定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSelectAttachmentPath_Click(object sender, EventArgs e)
        {
            if(_sendType == Constants.SEND_TYPE.REGULAR_SENDING)
            {
                // 添付ファイルが保存されているフォルダを選択するダイアログを表示
                using (var dlg = new FolderBrowserDialog())
                {
                    // ダイアログ設定
                    dlg.Description = "フォルダを選択してください";
                    dlg.RootFolder = Environment.SpecialFolder.MyComputer;
                    // テキストボックスに既に有効なパスがあればそれを起点にする
                    var tbPath = txtAttachmentPath.Text;
                    if (!string.IsNullOrWhiteSpace(tbPath) && Directory.Exists(tbPath))
                    {
                        dlg.SelectedPath = tbPath;
                    }
                    // ダイアログ表示
                    if (dlg.ShowDialog() == DialogResult.OK)
                    {
                        // 選択されたフォルダパスをテキストボックスにセット
                        txtAttachmentPath.Text = dlg.SelectedPath;
                    }
                }
            }
            else
            {
                // 添付ファイルを選択するダイアログを表示
                using (var dlg = new OpenFileDialog())
                {
                    dlg.Title = "ファイルを選択してください";
                    dlg.Filter = "すべてのファイル (*.*)|*.*";
                    dlg.CheckFileExists = true;
                    dlg.CheckPathExists = true;

                    // 既にパスが入っていれば、そのディレクトリを起点に
                    if (!string.IsNullOrWhiteSpace(txtAttachmentPath.Text))
                    {
                        try
                        {
                            dlg.InitialDirectory = Path.GetDirectoryName(txtAttachmentPath.Text);
                        }
                        catch { /* 無効なパスは無視 */ }
                    }

                    if (dlg.ShowDialog() == DialogResult.OK)
                    {
                        txtAttachmentPath.Text = dlg.FileName;
                    }
                }
            }

        }
    }
}