2016-12-13 91 views
1

我正在編寫控制檯應用程序。 它做在窗口的任務欄這個應用程序的以下在最上面打開的Windows資源管理器中創建新文件夾

  1. 我把(銷)EXE。

  2. 我會打開Windows資源管理器或可能已經有很多打開的窗口之前 窗口。

  3. ,我會選擇一個窗口,在這裏我想創建一個文件夾 (文件夾層次)

  4. ,我要點擊該圖標從我的EXE 步驟固定的任務欄1.

我試過下面的代碼。

它正在工作,當我通過使用Windows調度程序安排它。每5分鐘後在最上面的窗口中創建一個新文件夾。

然而,當我用它來牽制任務欄後執行EXE嘗試,它不工作..

請讓我知道是否有任何解決方案。

注 - 有用的,如果你告訴我如何創建新建自定義文件夾後,一個新的選擇右鍵單擊

using System; 
using System.Text; 

namespace CreateNewFolders 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      try 
      { 
       // get the active window 
       IntPtr handle = GetForegroundWindow(); 

       // Required ref: SHDocVw (Microsoft Internet Controls COM Object) - C:\Windows\system32\ShDocVw.dll 
       SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindows(); 

       // loop through all windows 
       foreach (SHDocVw.InternetExplorer window in shellWindows) 
       { 
        Console.WriteLine(((int)handle).ToString()); 
        Console.WriteLine(window.HWND.ToString()); 
        if (window.HWND == (int)handle) 
        { 
         // Required ref: Shell32 - C:\Windows\system32\Shell32.dll 
         var shellWindow = window.Document as Shell32.IShellFolderViewDual2; 

         // will be null if you are in Internet Explorer for example 
         if (shellWindow != null) 
         { 
          // Item without an index returns the current object 
          var currentFolder = shellWindow.Folder.Items().Item(); 

          // special folder - use window title 
          // for some reason on "Desktop" gives null 
          if (currentFolder == null || currentFolder.Path.StartsWith("::")) 
          { 
           // Get window title instead 
           const int nChars = 256; 
           StringBuilder Buff = new StringBuilder(nChars); 
           //if (GetWindowText(handle, Buff, nChars) > 0) 
           //{ 
           // return Buff.ToString(); 
           //} 
          } 
          else 
          { 

           System.IO.Directory.CreateDirectory(currentFolder.Path + "//NewFolder//Db_Scripts"); 
           System.IO.Directory.CreateDirectory(currentFolder.Path + "//NewFolder//Documents"); 
           System.IO.Directory.CreateDirectory(currentFolder.Path + "//NewFolder//SourceCode"); 
           // return currentFolder.Path; 
          } 
         } 

         break; 
        } 
       } 


      } 
      catch (Exception ex) 
      { 
       Console.Write(ex.ToString()); 
       Console.Read(); 
      } 
      //Console.Read(); 
     } 
     [System.Runtime.InteropServices.DllImport("user32.dll")] 
     private static extern IntPtr GetForegroundWindow(); 

     [System.Runtime.InteropServices.DllImport("user32.dll")] 
     static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count); 

    } 
} 

引用從COM加入C:/windows/system32/Shell32Microsoft Internet Controls

+0

用autohotkey做,它更簡單,你可以創建一個執行動作的熱鍵。 – C1sc0

+0

@ C1sc0對不起。沒有使用外部工具的權限 – captainsac

+0

也許它在你盯着任務欄時失去了焦點,你可以爲自己製作一個類似於應用程序的autohotkey(http://www.pinvoke.net/default.aspx/user32/SetWindowsHookEx.html )或者將你的小應用程序放在上下文菜單中(只需要在註冊表中添加一行) – C1sc0

回答

1

此代碼適用於我,僅添加2行。在它執行之前更改回上次訪問的窗口。

using System; 
using System.IO; 
using System.Runtime.InteropServices; 
using System.Text; 
using System.Threading; 
using System.Windows.Forms; 

namespace lastActiveWindow 
{ 
    internal class Program 
    { 
     private static void Main(string[] args) 
     { 
      // New lines.. 
      SendKeys.SendWait("%{Tab}"); 
      Thread.Sleep(500); 
      try 
      { 
       // get the active window 
       var handle = GetForegroundWindow(); 

       // Required ref: SHDocVw (Microsoft Internet Controls COM Object) - C:\Windows\system32\ShDocVw.dll 
       SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindows(); 

       // loop through all windows 
       foreach (SHDocVw.InternetExplorer window in shellWindows) 
       { 
        Console.WriteLine(((int) handle).ToString()); 
        Console.WriteLine(window.HWND.ToString()); 
        if (window.HWND == (int) handle) 
        { 
         // Required ref: Shell32 - C:\Windows\system32\Shell32.dll 
         var shellWindow = window.Document as Shell32.IShellFolderViewDual2; 

         // will be null if you are in Internet Explorer for example 
         if (shellWindow != null) 
         { 
          // Item without an index returns the current object 
          var currentFolder = shellWindow.Folder.Items().Item(); 

          // special folder - use window title 
          // for some reason on "Desktop" gives null 
          if (currentFolder == null || currentFolder.Path.StartsWith("::")) 
          { 
           // Get window title instead 
           const int nChars = 256; 
           var Buff = new StringBuilder(nChars); 
           //if (GetWindowText(handle, Buff, nChars) > 0) 
           //{ 
           // return Buff.ToString(); 
           //} 
          } 
          else 
          { 
           Directory.CreateDirectory(currentFolder.Path + "//NewFolder//Db_Scripts"); 
           Directory.CreateDirectory(currentFolder.Path + "//NewFolder//Documents"); 
           Directory.CreateDirectory(currentFolder.Path + "//NewFolder//SourceCode"); 
           // return currentFolder.Path; 
          } 
         } 

         break; 
        } 
       } 
      } 
      catch (Exception ex) 
      { 
       Console.Write(ex.ToString()); 
       Console.Read(); 
      } 
      //Console.Read(); 
     } 

     [DllImport("user32.dll")] 
     private static extern IntPtr GetForegroundWindow(); 

     [DllImport("user32.dll")] 
     private static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count); 
    } 
} 
相關問題