Take the file path opened by the process name in C#

Asked

Viewed 440 times

3

I have an application that works based on the client’s current process. I need to get the name of the file and its absolute path when opened. That is to say:

If for example the client opens the "test.txt" file, I need to get the path "c: project test.txt" instead of "c: windows system32 Notepad.exe". The same goes for . DOC, . XLS, etc...

The code snippet below returns the process-related information, which includes the software path (... Notepad.exe).

[DllImport("user32.dll", EntryPoint = "GetForegroundWindow", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
public static extern int GetForegroundWindow();

[...]

public static string GetFilePathName()
{
    uint procId = 0;
    int hWnd = GetForegroundWindow();
    GetWindowThreadProcessId(hWnd, out procId);
    var proc = Process.GetProcessById((int)procId);
}

Through the Process class it is possible to acquire the file path that is being worked?

  • The execution environment only has information about the process, not about files the application is manipulating. This is the context of the application, not the process. What object do you have to manipulate the file?

1 answer

1

Through the Process class it is possible to acquire the file path that is being worked?

No. For this case, the approach needs to be another, using a . NET class called FileSystemWatcher. I have taken the example below:

m_starting_path.Text = "C:\\
watcher = new FileSystemWatcher();

//---------------------------------------------------------------
// Suponho que seja uma aplicação que tenha um botão
// Aqui ocorre o acionamento do FileSystemWatcher, que 
// funciona basicamente com eventos. 

private void MeuBotaoClick(object sender, System.EventArgs e)
{
    if (watcher.EnableRaisingEvents == true) {
        MessageBeep(100);
        return
    }

    watcher.Path         = m_starting_path.Text;
    watcher.Filter       = m_filter.Text;
    watcher.NotifyFilter = NotifyFilters.FileName |
                           NotifyFilters.Attributes |
                           NotifyFilters.LastAccess |
                           NotifyFilters.LastWrite |
                           NotifyFilters.Security |
                           NotifyFilters.Size;


    watcher.Changed += new FileSystemEventHandler(OnFileEvent);
    watcher.Created += new FileSystemEventHandler(OnFileEvent);
    watcher.Deleted += new FileSystemEventHandler(OnFileEvent);
    watcher.Renamed += new RenamedEventHandler(OnRenameEvent);
    watcher.IncludeSubdirectories = true;
    watcher.EnableRaisingEvents = true;
}

public void OnFileEvent(object source, FileSystemEventArgs fsea)
{
    // Aqui você verifica modificações no arquivo (source)
}

public void OnRenameEvent(Object source, RenamedEventArgs rea)
{
    // Aqui você verifica arquivos renomeados
}

To relate the process to the file, you can use one of the solutions proposed in this OS question.

  • Gypsy, in this case my application automatically captures the process being executed. For example: if I have Notepad open by changing the "Test.txt" file, it captures this event. What I need is to also capture the Path of the "Test.txt" file at this very moment.

  • Because then, to get the path you use the first part of the question. To relate the Handle to the file, the link of the OS. What is the doubt?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.