How to get the current open file's path using C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kof0018
    New Member
    • Apr 2012
    • 2

    How to get the current open file's path using C#

    Hi, all

    I have a questions to obtain the current open file's path using C#.

    And I try using the Process.GetProc esses().MainWin dowTitle, it jsut get the opened file's name, not the opened file's path.

    Dose anyone has any idea can help me to get the complete file path?

    for example:

    I'm open the "test.txt" in the "C:\Users\Publi c\TestFolder."

    Now using the "Process.GetPro cesses().MainWi ndowTitle" just can obtain the "test.txt", I have no idea to get the "C:\Users\Publi c\TestFolder."

    Please help me.
    Thanks in advance.
  • Monomachus
    Recognized Expert New Member
    • Apr 2008
    • 127

    #2
    Show us the code and by the way if it is about File and you use OpenFileDialog to choose the File than just use code sample below
    Code:
    OpenFileDialog ofd = new OpenFileDialog(); 
    
    string filename = ""; 
    string path = ""; 
    
             if (ofd.ShowDialog() == DialogResult.OK) 
             { 
                        filename = System.IO.Path.GetFileName(ofd.FileName); 
                        path = System.IO.Path.GetDirectoryName(ofd.FileName); 
             } 
    
             MessageBox.Show(filename, "Filename"); 
             MessageBox.Show(path, "Directory");

    Comment

    • kof0018
      New Member
      • Apr 2012
      • 2

      #3
      Hi,

      Sorry, I don't describe clear.
      I'm open the file "test.txt" manual (click the mouse left button twice) and want to detect the opened file's path by programming.

      Thanks in advance.

      Comment

      Working...