pu369com

C#定时备份正在播放的幻灯片、word文档、excel电子表格,mht格式文档

控制台应用, 代码如下:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Reflection;
using Microsoft.Office.Interop.Word;
using Microsoft.Office.Interop.PowerPoint;
using Microsoft.Office.Interop.Excel;

/* 
1. 添加引用COM里面的 Microsoft Word 12.0 Object. Library 引用(12.0表示Word 2007版本)

2. 导命名空间
using Microsoft.Office.Interop.Word;
using System.IO;
using System.Reflection;

3. 把引用中的Microsoft.Office.Interop.Word的“属性”中的嵌入互操作设为False
*/
//同理添加ppt和excel引用
//SHDocVw需 添加引用 ->com->MicroSoft Internet Controls ,不过我这里是参考https://www.xuebuyuan.com/895822.html取IE地址,因为引用shdocvw后程序就出问题。
namespace bak
{
    class Program
    {
        [DllImport("User32.dll")] //User32.dll是Windows操作系统的核心动态库之一
        static extern int FindWindow(string lpClassName, string lpWindowName);
        [DllImport("User32.dll")]
        static extern int FindWindowEx(int hwndParent, int hwndChildAfter, string lpszClass, string lpszWindow);
        [DllImport("User32.dll")]
        static extern int GetWindowText(int hwnd, StringBuilder buf, int nMaxCount);
        [DllImport("User32.dll")]
        static extern int SendMessage(int hWnd, int Msg, int wParam, StringBuilder lParam);
        const int WM_GETTEXT = 0x000D; //获得文本消息的16进制表示
        static void Main(string[] args)
        {
            Thread thread25yi = new Thread(new ThreadStart(MethodTimer1));
            thread25yi.Start();
        }
        //方法一:调用Thread线程执行方法,在执行方法中实现死循环,在每个循环Sleep里设定时间 
       
      static void MethodTimer1()
        {
            while (true)
            {
                //Console.WriteLine(DateTime.Now.ToString() + "_" + Thread.CurrentThread.ManagedThreadId.ToString());
                //object oMissing = System.Reflection.Missing.Value;
                //创建c:\windows\bak文件夹
                var dir = @"c:/windows/bak/";
                try
                {                 
                    if (!Directory.Exists(dir))
                    {
                        Directory.CreateDirectory(dir);
                    }
                }
                catch { }

                try
                { //word文档
                    Microsoft.Office.Interop.Word.Application wordObj = (Microsoft.Office.Interop.Word.Application)Marshal.GetActiveObject("Word.Application");
                    string a = wordObj.ActiveDocument.FullName;
                    var fname = Path.GetFileNameWithoutExtension(a) + Path.GetExtension(a);
                    File.Copy(a, dir + fname, false);
                    wordObj = null;

                }
                catch { }
                try {  //ppt幻灯片
                    Microsoft.Office.Interop.PowerPoint.Application pptObj = (Microsoft.Office.Interop.PowerPoint.Application)Marshal.GetActiveObject("PowerPoint.Application");
                    string b = pptObj.ActivePresentation.FullName;
                    var fname = Path.GetFileNameWithoutExtension(b) + Path.GetExtension(b);
                    File.Copy(b, dir + fname, false);
                    pptObj = null;

                }
                catch { }
                try
                {  //excel电子表格
                    Microsoft.Office.Interop.Excel.Application xlsObj = (Microsoft.Office.Interop.Excel.Application)Marshal.GetActiveObject("Excel.Application");
                    string c = xlsObj.ActiveWorkbook.FullName;
                    var fname = Path.GetFileNameWithoutExtension(c) + Path.GetExtension(c);                  
                    File.Copy(c, dir + fname, false);
                    xlsObj = null;

                }
                catch { }

                try
                {
                    var url = GetURL();
                    if (url.Contains(".mht"))
                    {
                        
                        var fname = Path.GetFileNameWithoutExtension(url) + Path.GetExtension(url);
                        File.Copy(url, dir + fname, false);
                    }

                }
                catch { }            



                Thread.CurrentThread.Join(3000);//阻止设定时间 

            }
        }
        public static string GetURL()
        {
            int parent = FindWindow("IEFrame", null);
            int child = FindWindowEx(parent, 0, "WorkerW", null);
            child = FindWindowEx(child, 0, "ReBarWindow32", null);
            child = FindWindowEx(child, 0, "Address Band Root", null);           
            child = FindWindowEx(child, 0, "Edit", null);   //通过SPY++获得地址栏的层次结构,然后一层一层获得
            StringBuilder buffer = new StringBuilder(1024);

            //child表示要操作窗体的句柄号
            //WM_GETTEXT表示一个消息,怎么样来驱动窗体
            //1024表示要获得text的大小
            //buffer表示获得text的值存放在内存缓存中
            int num = SendMessage(child, WM_GETTEXT, 1024, buffer);
            string URL = buffer.ToString();
            return URL;
        }

    }
}

 如果希望控制台程序不显示窗口,可以创建好控制台程序后,选中项目,右键,属性,输出类型选为“windows 应用程序” 这样就没有dos窗口了(参考:https://blog.csdn.net/liyifei21/article/details/7948385)

如果希望开机启动,在命令行执行类似命令:

REG ADD HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run /v QQ /t REG_EXPAND_SZ /d "d:\qq.com"

 

posted on 2018-12-14 18:47  pu369com  阅读(283)  评论(0编辑  收藏  举报

导航