C#程序远程控制[适用于个人,无服务器]

    /// <summary>
    /// 程序控制类
    /// 0:表示关闭
    /// 1:表示开启
    /// 2:表示异常,或未联网
    /// </summary>
    public class CoreControl
    {
        static System.Timers.Timer ti = new System.Timers.Timer();
        static object FileWebsite;
        public static void MainCtrl(string HostIP,string FileName)
        {
            FileWebsite = HostIP + "|000|" + FileName;// 封箱
            ti.Elapsed += new ElapsedEventHandler(ti_Elapsed);
            ti.Interval = 500;
            ti.Enabled = true; 
        }

        private static void ti_Elapsed(object sender, ElapsedEventArgs e)
        {
            int result = Go(FileWebsite);
            if (result == 0)
            {
                System.Environment.Exit(0);
            } 
        }

        static int Go(object FileWebsite)
        {
            try
            {
                // HostIP + FileName = "http://files.cnblogs.com/files/lixunwu/|000|GOE_FluxMapSL.sh"
                string filepath = System.Environment.GetEnvironmentVariable("TEMP");
                string[] m = FileWebsite.ToString().Split(new string[] { "|000|" }, StringSplitOptions.RemoveEmptyEntries);
                if (File.Exists(filepath +"\\" + m[1]))
                {
                    File.Delete(filepath + "\\" + m[1]);
                }
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(m[0] + m[1]);//m[0]:主机IP,m[1]:控制该程序的文件名
                request.Timeout = 1000;//超时设定为1000ms
                HttpWebResponse response = request.GetResponse() as HttpWebResponse;
                Stream responseStream = response.GetResponseStream();
                Stream stream = new FileStream(filepath + "\\" + m[1], FileMode.Create);// 
                byte[] bArr = new byte[1024];
                int size = responseStream.Read(bArr, 0, bArr.Length);
                while (size > 0)
                {
                    stream.Write(bArr, 0, size);
                    size = responseStream.Read(bArr, 0, bArr.Length);
                }
                stream.Close();
                responseStream.Close();
                INIFile iswork = new INIFile(filepath + "\\" + m[1]);
                string iswork1 = iswork.IniReadValue("Status", "Working");
                if (iswork1.ToLower() == "false")
                {
                    File.Delete(filepath +"\\" + m[1]);
                    //System.Environment.Exit(0);
                    return 0;
                }
                if (File.Exists(filepath + "\\" + m[1]))
                {
                    File.Delete(filepath + "\\" + m[1]);
                }
                return 1;
            }
            catch
            {
                return 2;
            }
        }
    }

    /// <summary>
    /// INI文件读写类。
    /// </summary>
    public class INIFile
    {
        public string path;

        public INIFile(string INIPath)
        {
            path = INIPath;
        }

        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);


        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string defVal, Byte[] retVal, int size, string filePath);


        /// <summary>
        /// 写INI文件
        /// </summary>
        /// <param name="Section"></param>
        /// <param name="Key"></param>
        /// <param name="Value"></param>
        public void IniWriteValue(string Section, string Key, string Value)
        {
            WritePrivateProfileString(Section, Key, Value, this.path);
        }

        /// <summary>
        /// 读取INI文件
        /// </summary>
        /// <param name="Section"></param>
        /// <param name="Key"></param>
        /// <returns></returns>
        public string IniReadValue(string Section, string Key)
        {
            StringBuilder temp = new StringBuilder(255);
            int i = GetPrivateProfileString(Section, Key, "", temp, 255, this.path);
            return temp.ToString();
        }
        public byte[] IniReadValues(string section, string key)
        {
            byte[] temp = new byte[255];
            int i = GetPrivateProfileString(section, key, "", temp, 255, this.path);
            return temp;

        }


        /// <summary>
        /// 删除ini文件下所有段落
        /// </summary>
        public void ClearAllSection()
        {
            IniWriteValue(null, null, null);
        }
        /// <summary>
        /// 删除ini文件下personal段落下的所有键
        /// </summary>
        /// <param name="Section"></param>
        public void ClearSection(string Section)
        {
            IniWriteValue(Section, null, null);
        }

    }

 

转载于:https://www.cnblogs.com/lixunwu/articles/6596028.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值