示例#1
0
        private void txtOptions_ButtonClicked(object sender, EventArgs e)
        {
            try
            {
                #region Sanity Checks
                if (string.IsNullOrEmpty(Filename))
                {
                    throw new ArgumentNullException("Filename");
                }
                string ext = Path.GetExtension(Filename).Substring(1).ToUpper();
                if (!ImportManager.IsSupportedExtension(ext))
                {
                    throw new ArgumentException("File extension not supported: " + ext);
                }
                #endregion

                var t = ImportManager.GetOptionType(ext);
                if (t == null)
                {
                    throw new ArgumentException("Option type not found for extension: " + ext);
                }

                #region Create DataTable
                var dt = new DataTable("Options");
                dt.Columns.Add("Key", typeof(string)).ReadOnly = true;
                dt.Columns.Add("Value", typeof(string));
                dt.Columns.Add("DataType", typeof(string)).ReadOnly = true;

                var piList = t.GetProperties();
                foreach (var item in piList)
                {
                    var dr = dt.NewRow();
                    dr["Key"]      = item.Name;
                    dr["Value"]    = DBNull.Value;
                    dr["DataType"] = item.PropertyType.Name;
                    dt.Rows.Add(dr);
                }
                #endregion

                using (var d = new DynamicEditorForm())
                {
                    d.Caption    = "Import Option(s) Editor";
                    d.DataSource = dt;
                    if (d.ShowDialog() != DialogResult.OK)
                    {
                        return;
                    }
                }

                var sb = new StringBuilder();

                foreach (DataRow dr in dt.Rows)
                {
                    if (DataConvert.IsNull(dr["Value"]))
                    {
                        continue;
                    }

                    var key  = dr["Key"].ToString();
                    var val  = dr["Value"].ToString();
                    var type = dr["DataType"].ToString();

                    if (type.Equals("String", StringComparison.InvariantCultureIgnoreCase))
                    {
                        val = string.Format("\"{0}\"", val);
                    }

                    if (sb.Length > 0)
                    {
                        sb.Append("; ");
                    }

                    sb.AppendFormat("{0}={1}", key, val);
                }

                Options = sb.ToString();
            }
            catch (Exception ex)
            {
                FormUtil.WinException(ex);
            }
        }