1

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?
}
spaghettifunk
  • 1,936
  • 4
  • 24
  • 46
user3354197
  • 95
  • 2
  • 11

4 Answers4

2

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)
dkozl
  • 32,814
  • 8
  • 87
  • 89
2

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.       
}
Anders Forsgren
  • 10,827
  • 4
  • 40
  • 77
  • This code is good except the GetDocumentFromActvieTab is throwing a error and saying a class doesnt exist for it? – user3354197 Mar 02 '14 at 10:55
  • Am i right in adding this class to the desighner? I have no clue where to put this class. I have it working except the Tab variable on the left I added a refrence for it and got a error Error 1 'System.Windows.Forms.VisualStyles.VisualStyleElement.Tab': static types cannot be used as type arguments c:\users\******\documents\visual studio 2013\Projects\My Note 1.0\My Note 1.0\Form1.Designer.cs 203 32 My Note 1.0 – user3354197 Mar 02 '14 at 11:04
  • I didn't mean "Tab" explicitly, it may be called something else (TabView, TabPage etc) depending on which UI framework you are using (e.g. Windows forms, WPF). I was deliberately making the code very vague. I think you should probably get up to speed on Object oriented concepts before diving in with trying to do a full GUI app. Start with reading e.g about MVVM http://stackoverflow.com/questions/1405739/mvvm-tutorial-from-start-to-finish – Anders Forsgren Mar 02 '14 at 11:21
0
// 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

Dexters
  • 2,419
  • 6
  • 37
  • 57
0

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 ''
*/
Derek
  • 7,615
  • 5
  • 33
  • 58