2014年1月16日木曜日

PC再起動時に,前回開いていたPower Pointファイルをまとめて開く

Windows Updateをかけると,PCの再起動を迫られることがよくある.
何の因果かPower Pointを5枚も10枚も開いていた場合,再起動した後にそれらをひとつひとつ開き直すのはめんどくさい. 特にファイルの保管場所がバラバラだとこの上なくめんどくさい.
そこで,現在開いているPower Pointファイルの場所(ファイルパス)を記憶しておいて,再起動後にそれらをまとめて開くためのプログラムを書いた. 言語はC#.(実はC#触るの初めてなので環境構築からだった)
仕様は,
  • 起動するとコンソール上に「save」「open」のメニューが出る
  • saveを選ぶと,現在開いているpptファイルのパス一覧を表示し,ユーザのデスクトップフォルダに保存する.
  • openを選ぶと,前回デスクトップに保存したファイルからパスを読み込んで,まとめて開いてくれる
という感じ.で,コードは以下. 命名とか色々適当なのはスルーで.

using System;
using System.Text;
using System.IO;
using System.Collections.Generic;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Microsoft.VisualBasic;

namespace PPTSO{
    class PPT{
        private string save_file = "C:\\Users\\" + System.Environment.UserName + "\\Desktop\\PPTSO.txt";

        static void Main(string[] args){
            PPT ppt = new PPT();
            if (!File.Exists(ppt.save_file)){
                File.Create(ppt.save_file).Close();
            }
            while (true){
                Console.Clear();
                Console.WriteLine("s: save current ppt files");
                Console.WriteLine("o: open previous ppt files");
                Console.WriteLine("q: quit");
                Console.Write("input: ");
                string menu = Console.ReadLine();
                Console.WriteLine(menu);
                if (menu.Equals("s")){
                    ppt.savePPT();
                }else if (menu.Equals("o")){
                    ppt.openPPT();
                }else if (menu.Equals("q")){
                    break;
                }else{
                    Console.WriteLine("input error. press any key...");
                    Console.ReadKey();
                }
            }
        }

        public void savePPT(){
            Interaction.CreateObject("PowerPoint.Application", "");
            PowerPoint.Application pApp = (PowerPoint.Application)Interaction.GetObject(null, "PowerPoint.Application");
            foreach (PowerPoint.DocumentWindow dw in pApp.Windows){
                Console.WriteLine("{0}", dw.Presentation.FullName);
            }
            string yn;
            Console.WriteLine("--------------------");
            Console.WriteLine("are you sure to save? y/n");
            while (true){
                yn = Console.ReadLine();
                if (yn.Equals("n")){
                    return;
                }else if (yn.Equals("y")){
                    break;
                }else{
                    Console.WriteLine("input error. press \"y\" or \"n\":");
                }
            }
            FileStream fs = new FileStream(save_file, FileMode.Create);
            StreamWriter sw = new StreamWriter(fs);
            foreach (PowerPoint.DocumentWindow dw in pApp.Windows){
                sw.Write(dw.Presentation.FullName + "\r\n");
            }
            sw.Close();
            fs.Close();
        }

        public void openPPT(){
            string path;
            List<string> pathList = new List<string>();
            FileStream fs = new FileStream(save_file, FileMode.Open);
            StreamReader sr = new StreamReader(fs);
            while ((path = sr.ReadLine()) != null){
                pathList.Add(path);
                Console.WriteLine(path);
            }
            sr.Close();
            fs.Close();
            string yn;
            Console.WriteLine("--------------------");
            Console.WriteLine("are you sure to open? y/n");
            while (true){
                yn = Console.ReadLine();
                if (yn.Equals("n")){
                    return;
                }else if (yn.Equals("y")){
                    break;
                }else{
                    Console.WriteLine("input error. press \"y\" or \"n\":");
                }
            }
            List<string> openedPathList = new List<string>();
            Interaction.CreateObject("PowerPoint.Application", "");
            PowerPoint.Application pApp = (PowerPoint.Application)Interaction.GetObject(null, "PowerPoint.Application");
            foreach (PowerPoint.DocumentWindow dw in pApp.Windows){
                openedPathList.Add(dw.Presentation.FullName);
            }
            PowerPoint.Presentations pPt = new PowerPoint.Application().Presentations;
            foreach (string str in pathList){
                if (openedPathList.Contains(str)) continue;
                pPt.Open(str);
            }
        }
    }
}








0 件のコメント:

コメントを投稿