⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainform.cs

📁 WCF文件传输示例
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.ServiceModel;
using System.Threading;

namespace FileService
{
    public partial class MainForm : Form, ILog
    {
        ServiceHost _myServiceHost;
        AppParam _appParam;

        public delegate bool FormAddLog(string info);
        FormAddLog _formAddLog;

        public MainForm()
        {
            InitializeComponent();

            _formAddLog = new FormAddLog(this.Log);

            _appParam = AppValue.GetParam();
            AppParam.Load(ref _appParam);
            tbSaveDir.Text = _appParam._saveDir;

        }

        public void host_Closed(object sender, EventArgs e)
        {
            Program.Get_ILog().Log("host_Closed"+e.ToString());
        }
        public void host_Closing(object sender, EventArgs e)
        {
            Program.Get_ILog().Log("host_Closing" + e.ToString());
        }
        public void host_Faulted(object sender, EventArgs e)
        {
            Program.Get_ILog().Log("host_Faulted" + e.ToString());
        }

        void ServerStart()
        {
            try
            {
                _myServiceHost = new ServiceHost(typeof(FileService2.FileSave));
                _myServiceHost.Closed += new EventHandler(host_Closed);
                _myServiceHost.Closing += new EventHandler(host_Closing);
                _myServiceHost.Faulted += new EventHandler(host_Faulted); 

                _myServiceHost.Open();
            }
            catch (Exception ex)
            {
                Program.Get_ILog().Log(ex.Message);
                return;
            }
            Program.Get_ILog().Log("启动成功");
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            if (_myServiceHost == null)
            {
                Thread threadRead = new Thread(new ThreadStart(ServerStart));
                threadRead.Start();
                btnStart.Text = "停止服务";
            }
            else
            {
                _myServiceHost.Close();
                _myServiceHost = null;
                 btnStart.Text = "启动服务";
                Program.Get_ILog().Log("停止服务");
            }
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            InitLog();
        }

        void InitLog()
        {
            //进程
            Rectangle rc = lvLog.ClientRectangle;
            lvLog.Clear();		//clear control

            int n = (rc.Width - 17) / 5;
            lvLog.Columns.Add("序号", n, System.Windows.Forms.HorizontalAlignment.Left);
            lvLog.Columns.Add("信息", n * 3, System.Windows.Forms.HorizontalAlignment.Left);
            lvLog.Columns.Add("时间", n, System.Windows.Forms.HorizontalAlignment.Left);

        }

        public bool Log(string info)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(_formAddLog, info);
                return true;
            }
            string[] lvData = new string[3];
            lvData[0] = (lvLog.Items.Count + 1).ToString();
            lvData[1] = info;
            lvData[2] = DateTime.Now.ToString();
            ListViewItem lvItem = new ListViewItem(lvData);
            lvLog.Items.Add(lvItem);
            return true;
        }

        private void btnSaveDir_Click(object sender, EventArgs e)
        {

            FolderBrowserDialog dir = new FolderBrowserDialog();
            if (dir.ShowDialog() == DialogResult.OK)
            {
                tbSaveDir.Text = dir.SelectedPath;
                _appParam._saveDir = tbSaveDir.Text;
                AppParam.Save(_appParam);
            }
            AppParam _appParam3 = AppValue.GetParam();
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -