浅谈C#创建可扩展应用程序(二)

本文通过代码展示了如何使用C#构建一个可扩展的应用程序,主要涉及配置文件和异常处理。程序通过XML配置文件管理不同类型的文件处理,如C#、C++等,并在主程序中设置异常处理机制,确保程序的稳定运行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 本想把系统逻辑图贴出来,贴个图可真难,最终还是没贴出来 :(
我就先贴出来一些代码了,在后面讲解逻辑与思路

下面这个是我们程序的配置文件中的内容:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    
<configSections>
    
<section name="EnumerSetting"
        type
="System.Configuration.SingleTagSectionHandler" />
    
</configSections>
    
    
<appSettings>
        
<!-- config the file type 
    
        config rule:
            AssemblyName[TypeName]
            
        example:
            <add key=".mytype" value = "MyAssembly[MyType]"/>
        
-->
        
        
<!-- csharp code enumer -->
        
<add key=".cs" value="CSharpEnumer[Enumer2006.CSharp.CSharpEnmer]"/>
        
        
<!-- c/c++ -->
        
<add key=".h" value="CSharpEnumer[Enumer2006.CSharp.CSharpEnmer]"/>
        
<add key=".cpp" value="CSharpEnumer[Enumer2006.CSharp.CSharpEnmer]"/>
        
<add key=".c" value="CSharpEnumer[Enumer2006.CSharp.CSharpEnmer]"/>
        
<!-- javascript -->
        
        
<!-- java -->
        
        
<!-- asp/asp.net -->
        
        
<!-- html/xml -->
        
        
<!-- delphi -->
    
</appSettings>
    
    
    
<EnumerSetting>    
        
    
</EnumerSetting>
</configuration>

下面是主程序代码:
using System;
using System.Windows.Forms;
using System.Threading;

namespace Enumer2006
{
    
/// <summary>
    
/// EnmerStart
    
/// </summary>

    public class EnmerStart
    
{
        [STAThread]
        
static void Main() 
        
{
            
// ThreadException
            Application.ThreadException += new
                ThreadExceptionEventHandler(Application_ThreadException);

            
// UnhandledException
            Thread.GetDomain().UnhandledException += new
                UnhandledExceptionEventHandler(Application_UnhandledException);

            
//ApplicationをRun            
            Application.Run(new Form1());
        
            
return;
        }


        
//************************************************************************
        /// <summary>
        
/// 
        
/// </summary>

        //************************************************************************
        public static void ShowErrorMessage(Exception ex)
        
{
            MessageBox.Show(null, ex.Message + " " + ex.StackTrace, "error",
                MessageBoxButtons.OK, MessageBoxIcon.Error);
            Application.Exit();
            
// append by linzc
        }

        
        
...
    }

}
下面是程序主窗体Form中的代码
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

using EnumerConfig.Config;
using EnumerConfig.Factories;
using EnumerUtility.CommonStruct;
using EnumerOutput;
using EnumerUtility.Excepiton;

namespace Enumer2006
{    
    
public class Form1 : System.Windows.Forms.Form
    
{
        
private System.Windows.Forms.Button btnAddFolder;
        
private System.Windows.Forms.Button btnAddFiles;
        
private System.Windows.Forms.Button btnAccount;
        
private System.Windows.Forms.Label lblOutInfo;
        
private System.Windows.Forms.ListBox lstFiles;
        
/// <summary>
        
/// 
        
/// </summary>

        private System.ComponentModel.Container components = null;
        
private System.Windows.Forms.Button btnClear;

        ArrayList mSrcFileInfo 
= new ArrayList(); 

        
public Form1()
        
{
            
//
            
// Windows 
            
//
            InitializeComponent();
        }


        
/// <summary>
        
/// 
        
/// </summary>

        protected override void Dispose( bool disposing )
        
{
            
if( disposing )
            
{
                
if (components != null
                
{
                    components.Dispose();
                }

            }

            
base.Dispose( disposing );
        }


        
Windows

        
        
private void btnAccount_Click(object sender, System.EventArgs e)
        
{
            ArrayList fileInfos 
= GetFileInfos();
            EnumerProxy.Exectue(fileInfos);

            
// show info
            try
            
{
                
this.Enabled = false;
                System.Diagnostics.Process pro 
= new System.Diagnostics.Process();
                pro.StartInfo 
= new System.Diagnostics.ProcessStartInfo(
                                    
"EXCEL.EXE",EnumerProxy.GetOutputFileName());
                pro.Start();
                
this.Enabled = true;
            }

            
catch
            
{    
                
this.Enabled = true;
                
throw new EnumerExcelNotFoundException();
            }

        }


        
private void btnAddFolder_Click(object sender, System.EventArgs e)
        
{
            
this.Enabled = false;
            
            System.Windows.Forms.FolderBrowserDialog folderDlg 
= 
                
new FolderBrowserDialog();
            
            
if (folderDlg.ShowDialog() == DialogResult.OK)
            
{
                GetFileFromFolder(folderDlg.SelectedPath);
            }

            
this.Enabled = true;
        }


        
private void btnAddFiles_Click(object sender, System.EventArgs e)
        
{
            
this.Enabled = false;
            System.Windows.Forms.OpenFileDialog fileDlg 
= new OpenFileDialog();
            fileDlg.RestoreDirectory 
= true;
            fileDlg.Multiselect 
= true;

            
if (fileDlg.ShowDialog() == DialogResult.OK)
            
{
                
string [] files = fileDlg.FileNames;
                
for (int i = 0; i < files.Length ; ++i)
                
{
                    SourceFileInfo fileInfo 
= new SourceFileInfo();
                    fileInfo.filePath 
= files[i];
                    
int lastDotIndex = files[i].LastIndexOf(".");
                    fileInfo.fileExandName 
= ((lastDotIndex==-1)?
                        
string.Empty:
                        files[i].Substring(lastDotIndex));

                    mSrcFileInfo.Add(fileInfo);

                    
// add to the list
                    this.lstFiles.Items.Add(files[i]);
                }

            }

            
this.Enabled = true;
        }


        
void GetFileFromFolder(string bootDir)
        
{
            
// get files.
            string [] files = System.IO.Directory.GetFiles(bootDir);
            
for (int i = 0; i < files.Length ; ++i)
            
{
                SourceFileInfo fileInfo 
= new SourceFileInfo();
                fileInfo.filePath 
= files[i];
                
int lastDotIndex = files[i].LastIndexOf(".");
                fileInfo.fileExandName 
= ((lastDotIndex==-1)?
                    
string.Empty:
                    files[i].Substring(lastDotIndex));

                mSrcFileInfo.Add(fileInfo);

                
// add to the list
                this.lstFiles.Items.Add(files[i]);
            }

            
// get child folder
            string [] dirs = System.IO.Directory.GetDirectories(bootDir);
            
for (int i = 0 ; i < dirs.Length ; ++i)
            
{
                GetFileFromFolder(dirs[i]);
            }

        }


        ArrayList GetFileInfos()
        
{
            SourceFileInfo [] fileInfos 
= new SourceFileInfo[0];
            
return this.mSrcFileInfo;
        }


        
private void btnClear_Click(object sender, System.EventArgs e)
        
{
            
this.mSrcFileInfo.Clear();
            
this.lstFiles.Items.Clear();
            
// delete output file.
            try
            
{
                EnumerProxy.Clear();
            }

            
catch(EnumerOutFileUseingException ex)
            
{
                MessageBox.Show(ex.Message);
            }

        }

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值