简体   繁体   中英

How to identify a string as filepath and open in file explorer when clicked?

I am relatively new to C#/Visual Studio 2015 application development, coming from Android. I am writing a chat application that also allows users to send files to each other. The file transfer functionality is in place; the file gets downloaded to a pre-set folder when received, and the file-path of that file is then shown in the chat box to the recipient. However, that file path is shown as though it were regular text.

How do I make it such that said file-path (and/or urls, ideally) appear as a clickable hyperlinks, that then open said file?

Any help or resources to be pointed to would be most appreciated!

If you create a linkLabel object to show the path, you can add a callback to the event LinkClicked and open a file explorer:

    private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
    {
        Process.Start("C:/");
    }

Here we go:

You should use Uri class to build your url from string:

string filePath = "C:\\example.txt";
Uri uri = new Uri(filePath);
return uri.AbsoluteUri;

Hope it helps;)

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