简体   繁体   中英

Get full path from short file name

Can i get the full path from a filename such as get the full directory path from test.txt, Or is there a way i can save it

The reason im asking this is im making a application like Notepad++ some of you may of heard of it. When changing the tab control tab I want the form's text to be the full directory while the tabs text is just filename.format

My so far code

private void tabControl1_TabIndexChanged(object sender, EventArgs e)
{
    if (tabControl1.SelectedTab.Text.StartsWith("New"))
    {
       int count = tabControl1.TabCount - 1;
       this.Text = tabControl1.Controls[count].Text + " - My Note 1.0";
    }
    //It is a directory and i need to make the forms text the path here?
}

You can use System.IO.Path.GetFullPath :

var fullPath = Path.GetFullPath("test.txt");

If you pass in a short file name, it is expanded to a long file name.

If c:\\temp\\newdir is the current directory, calling GetFullPath on a file name such as test.txt returns c:\\temp\\newdir\\test.txt.

And if you want to get path from that you use System.IO.Path.GetDirectoryName

var path = Path.GetDirectoryName(fullPath)

I think you should probably keep the full path and get the filename from the full path rather than the other way around. The key is to use a type representing a document, and let the tab view that document. If each tab refers to a document, and each document knows its full path, then you can get the short filename from the documents full path.

public class Document
{
    public string FullPath { get; set; } // Full path to file, null for unsaved
    public string FileName 
    {
       get { return Path.GetFileName(FullPath); }
    }
}

When a new tab is focused, get the document for the active tab and set the forms title from the FullPath of the document.

private void tabControl1_TabIndexChanged(object sender, EventArgs e)
{
    Document activeDoc = GetDocumentFromActiveTab(); 

    // Update win title with full path of active doc.
    this.Text = (activeDoc.FullPath ?? "Unsaved document") + " MyApp" + version;
}

EDIT:

The key here is of course the method GetDocumentFromActiveTab() which isn't shown. You need to implement the data structures that manage your documents, and connects them to tabs. I did not include that in the answer, you need to try yourself. One idea is to make a type representing the entire application state including all tabs and documents.

public class Workspace
{
   private Dictionary<SomeTypeOfView, Document> documentsOpenInViews;

   // Methods to register a document to a tab, get document for a tab
   // remove tab+document when tab is closed etc.       
}
// not sure if this is what you want

say your filename with path is

string strFFL = @"C:\path\filename.format";
Console.WriteLine(System.IO.Path.GetFileName(strFFL)); //->filename.format
Console.WriteLine(System.IO.Path.GetDirectoryName(strFFL)); //-> C:\path

http://msdn.microsoft.com/en-us/library/system.io.path.getfilename(v=vs.110).aspx

http://msdn.microsoft.com/en-us/library/system.io.path.getdirectoryname(v=vs.110).aspx

I think this is actually backwards. I think you want to keep a full path to the file and be able to display the filename only on the tab. So I think you want Path.GetDirectory name.

From: http://msdn.microsoft.com/en-us/library/system.io.path.getdirectoryname(v=vs.110).aspx

string filePath = @"C:\MyDir\MySubDir\myfile.ext";
string directoryName;
int i = 0;

while (filePath != null)
{
    directoryName = Path.GetDirectoryName(filePath);
    Console.WriteLine("GetDirectoryName('{0}') returns '{1}'",
        filePath, directoryName);
    filePath = directoryName;
    if (i == 1)
    {
        filePath = directoryName + @"\";  // this will preserve the previous path
    }
    i++;
}
/*
This code produces the following output:

GetDirectoryName('C:\MyDir\MySubDir\myfile.ext') returns 'C:\MyDir\MySubDir'
GetDirectoryName('C:\MyDir\MySubDir') returns 'C:\MyDir'
GetDirectoryName('C:\MyDir\') returns 'C:\MyDir'
GetDirectoryName('C:\MyDir') returns 'C:\'
GetDirectoryName('C:\') returns ''
*/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM