Can I add a file name to the link label and leave the path to the path?

I want to load file pathin link label control windows formsand display only the name in link label, but when clicked label, the file opens from the path.

I did this, but I need to map to the link labelwhole path to the file that I do not want.

private void linkPdfLabel__LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
    Process process = new Process();
    process.StartInfo.FileName = linkPdfLabel_.Text;
    process.Start();
}
+4
source share
1 answer

You can use one of two solutions. One of them is to use a property Tagto assign an absolute path to a file and use it when clicked.

code example:

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();
}

- Links, LinkCollection, Link. , .

:

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();
}
+3

Source: https://habr.com/ru/post/1688054/


All Articles