5

I want to load a file path in a link label control windows forms and display only the the name in the link label but when the label is clicked to open the file from the path.

I did this but I have to display in the link label the entire file path, witch I don't want to.

private void linkPdfLabel__LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
    Process process = new Process();
    process.StartInfo.FileName = linkPdfLabel_.Text;
    process.Start();
}
kame
  • 20,848
  • 33
  • 104
  • 159
Bogdan Sorin
  • 129
  • 3
  • 14

1 Answers1

4

You can consider using one of the 2 solutions. One of which is to use Tag property to assign an absolute file path and use it when clicked.

example code:

void CreateLink(string absoluteFilePath)
{
    _linkPdfLabel_.Tag = absoluteFilePath;
    _linkPdfLabel_.Text = Path.GetFileName(absoluteFilePath);
}

void linkPdfLabel__LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
    Process process = new Process();
    process.StartInfo.FileName = (string)linkPdfLabel_.Tag;
    process.Start();
}

Another way is to use a property called Links which is a LinkCollection which holds a collection of Link objects. You can use this to get the full link instead of the text.

example code:

void CreateFileLink(string absoluteFilePath)
{
    linkPdfLabel_.Text = Path.GetFileName(absoluteFilePath);

    var link = new LinkLabel.Link();
    link.LinkData = absoluteFilePath;
    linkPdfLabel_.Links.Add(link);
}

void linkPdfLabel__LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
    Process process = new Process();
    process.StartInfo.FileName = (string)linkPdfLabel_.Link[0].LinkData;
    process.Start();
}
mrogal.ski
  • 5,828
  • 1
  • 21
  • 30