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

📄 appparam.cs

📁 WCF文件传输示例
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Windows.Forms;
using System.Diagnostics;

namespace FileService
{
    
    class AppParam
    {
        [Serializable]
        public struct SaveParam
        {
          public string saveDir;
        } ;

        public SaveParam _saveParam;

        public AppParam()
        {
            _saveParam = new SaveParam();
            _saveDir = "d:\\FileSave";
        }

        public string _saveDir
        {
            get
            {
                return _saveParam.saveDir;
            }
            set
            {
                _saveParam.saveDir = value;
            }
        }
        static string GetCurDir()
        {
            string str = Process.GetCurrentProcess().MainModule.FileName;
            int n = str.LastIndexOf('\\');
            if (n >= 0)
            {
                str = str.Remove(n, str.Length - n);
            }
            return str;
        }

        static public bool Save(AppParam param)
        {
            FileStream fs = null;
            try
            {
                BinaryFormatter formatter = new BinaryFormatter();
                string strDir = GetCurDir();

                fs = new FileStream(strDir + @"\FileSevice.dat", FileMode.Create);
                formatter.Serialize(fs, param._saveParam);
            }
            catch (Exception ex)
            {
                MessageBox.Show("保存参数失败!");
                return false;
            }
            finally
            {
                if (fs != null)
                    fs.Close();
            }
            return true;
        }

        static public bool Load(ref AppParam param)
        {
            FileStream fs = null;
            try
            {
                string strDir = GetCurDir();

                fs = new FileStream(strDir + @"\FileSevice.dat", FileMode.Open);

                BinaryFormatter formatter = new BinaryFormatter();

                // Deserialize the hashtable from the file and 
                // assign the reference to the local variable.
                param._saveParam = (AppParam.SaveParam)formatter.Deserialize(fs);
            }
            catch (Exception ex)
            {

                return false;
            }
            finally
            {
                if (fs != null)
                    fs.Close();
            }
            return false;
        }
    }
}

⌨️ 快捷键说明

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