2016-01-21 2 views
1

Comment récupérer le chemin réel du fichier du bloc-notes s'il est sauvegardé dans le lecteur. Par exemple, un processus bloc-notes est en cours d'exécution et il est enregistré quelque part dans le lecteur. Comment puis-je récupérer son chemin complet? En utilisant le code ci-dessous, je peux obtenir le détail du processus, mais pas le chemin réel de certains fichiers.Comment obtenir l'emplacement sauvegardé du fichier Notepad

Process[] localByName = Process.GetProcessesByName("notepad"); 
foreach (Process p in localByName) 
    { 
     string path = p.MainModule.FileName.ToString(); 
    } 

Ceci retourne le chemin exécutable mais j'ai besoin de l'emplacement du disque où réside le fichier actuel.

+0

Lorsque vous dites bloc-notes, voulez-vous dire un fichier .txt? Ou voulez-vous dire n'importe quel fichier qui est actuellement ouvert dans une instance de bloc-notes? –

+0

tout fichier ouvert dans le bloc-notes. – navi

+0

Vous savez que vous pouvez ouvrir le Bloc-notes sans un fichier? – LarsTech

Répondre

3

Cela devrait le faire:

 string wmiQuery = string.Format("select CommandLine from Win32_Process where Name='{0}'", "notepad.exe"); 
     ManagementObjectSearcher searcher = new ManagementObjectSearcher(wmiQuery); 
     ManagementObjectCollection retObjectCollection = searcher.Get(); 
     foreach (ManagementObject retObject in retObjectCollection) 
     { 
      string CommandLine = retObject["CommandLine"].ToString(); 
      string path = CommandLine.Substring(CommandLine.IndexOf(" ") + 1, CommandLine.Length - CommandLine.IndexOf(" ") - 1); 
     } 

Il ne fonctionnera que si le fichier est ouvert par un double clic ou par ligne de commande.

N'oubliez pas d'ajouter une référence à System.Management par le droit Cliquez sur Projet, Ajouter des références, puis sélectionnez l'onglet Assemblées et Rechercher System.Management.

Step 1

Step 2

+0

Je ne peux pas ajouter de référence pour ManagementObjectSearcher – navi

+0

@navi la réponse est éditée pour ajouter des images pour vous montrer comment ajouter une référence à 'System.Management' – Xenon

+0

Vous devrez également ajouter cette ligne à usings (le haut de votre code) "using System.Management;" – Xenon

1

Notepad ++ a un fichier session.xml dans le dossier% APPDATA% trouvé here.

Vous pouvez utiliser XDocument ou XPath pour analyser ce fichier et récupérer les chemins d'accès aux fichiers. Voici comment vous les obtenez avec XPath:

XmlDocument doc = new XmlDocument(); 
doc.Load(@"C:\Users\USERNAME_HERE\AppData\Roaming\Notepad++\session.xml"); 

XmlNodeList files = doc.SelectNodes("//NotepadPlus/Session/mainView/File"); 
foreach (XmlNode file in files) 
{ 
    Console.WriteLine(file.Attributes["filename"].Value); 
} 

S'il vous plaît noter que notepad ++ doit être fermé puis rouvert pour rafraîchir ce fichier.

+1

La question concerne Windows Notepad not Notepad ++ – Steve

+0

Oups. Je n'utilise jamais de bloc-notes ordinaire, donc j'ai toujours associé Notepad ++ au bloc-notes. Tant pis! – GabeMeister

0

Xenon's answer est certainement le meilleur si vous n'avez pas ouvert le fichier via Fichier-> Ouvrir dans le bloc-notes. Pour le plaisir, j'ai essayé de créer une solution qui fonctionnera dans tous les cas. Vous pouvez y parvenir en combinant Microsoft TestApi et UIAutomation. Fondamentalement, j'ouvre la boîte de dialogue Enregistrer sous ... pour obtenir le chemin du fichier actuellement ouvert. C'est moche mais ça marche! Remarque: Intaller le package nuget Microsoft.TestApi et ajouter des références à WindowsBase, System.Drawing, UIAutomationClient et UIAutomationTypes.

using Microsoft.Test.Input; 
using System; 
using System.Diagnostics; 
using System.Runtime.InteropServices; 
using System.Threading; 
using System.Windows; 
using System.Windows.Automation; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     [DllImport("user32.dll")] 
     static extern bool SetForegroundWindow(IntPtr hWnd); 

     static void Main(string[] args) 
     { 
     Process[] localByName = Process.GetProcessesByName("notepad"); 
     foreach (Process p in localByName) 
     { 
      string fileName = p.MainWindowTitle; // get file name from notepad title 
      SetForegroundWindow(p.MainWindowHandle); 
      AutomationElement windowAutomationElement = AutomationElement.FromHandle(p.MainWindowHandle); 

      var menuElements = windowAutomationElement.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.MenuItem)); 
      AutomationElement fileMenuElement = null; 
      foreach (AutomationElement element in menuElements) 
      { 
       if (element.Current.Name == "File") 
       { 
        fileMenuElement = element; 
        break; 
       } 
      } 

      if (fileMenuElement != null) 
      { 
       fileMenuElement.SetFocus(); 
       fileMenuElement.Click(); 
       Thread.Sleep(800); // Sleeping an arbitrary amount here since we must wait for the file menu to appear before the next line can find the menuItems. A better way to handle it is probably to run the FindAll in the next line in a loop that breaks when menuElements is no longer null. 
       menuElements = fileMenuElement.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.MenuItem)); 
       var saveAsMenuElement = fileMenuElement.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "Save As...")); 


       if (saveAsMenuElement != null) 
       { 
        saveAsMenuElement.SetFocus(); 
        saveAsMenuElement.Click(); 
        Thread.Sleep(800); 
        var saveAsWindow = windowAutomationElement.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Window)); 
        var toolbarElements = saveAsWindow.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.ToolBar)); 

        foreach (AutomationElement element in toolbarElements) 
         if (element.Current.Name.StartsWith("Address:")) 
          Console.WriteLine(element.Current.Name + @"\" + fileName); // Parse out the file name from this concatenation here! 
        var closeButtonElement = saveAsWindow.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "Close")); 
        closeButtonElement.Click(); 
       } 
      } 
     } 

     Console.ReadLine(); 
    } 
} 

public static class AutomationElementExtensions 
{ 
    public static void MoveTo(this AutomationElement automationElement) 
    { 
     Point somePoint; 
     if (automationElement.TryGetClickablePoint(out somePoint)) 
      Mouse.MoveTo(new System.Drawing.Point((int)somePoint.X, (int)somePoint.Y)); 
    } 
    public static void Click(this AutomationElement automationElement) 
    { 
     automationElement.MoveTo(); 
     Mouse.Click(MouseButton.Left); 
    } 
} 
}