Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a link to a text file in XAML

Tags:

wpf

xaml

How can I create a link in XAML to open a text file? The text file directory is same as the project directory.

like image 980
myID33 Avatar asked Apr 14 '14 18:04

myID33


People also ask

How to create a hyperlink in XAML?

In XAML, the creation of content elements is implicit, so you can add the link text directly to the Hyperlink, and the Hyperlink directly to the TextBlock element. The Span element with the xml:space="preserve" attribute is used to preserve white space around the hyperlink.

How do I add a link button in WPF?

Hello, Link button in ASP.NET at runtime works like a hyperlink, but you can write certain code with the use of a link button by generating its click event, in short the link button control looks like a hyperlink control but works like a button... Here is some code that might help you out.


2 Answers

Try this:

<TextBlock>
    <Hyperlink Click="openFile_Click">Open File</Hyperlink>
</TextBlock>

In order to get current application path you can use AppDomain.BaseDirectory Please notice that AppDomain.BaseDirectory includes "\" to the end of the path.

var appPath = System.AppDomain.CurrentDomain.BaseDirectory;

and to execute your file you can do:

private void openFile_Click(object sender, RoutedEventArgs e)
{
    System.Diagnostics.Process.Start(appPath + "myFile.txt");
}
like image 160
Vlad Bezden Avatar answered Nov 15 '22 11:11

Vlad Bezden


XAML

<TextBlock>
    <Hyperlink Click="Hyperlink_Click">Click Me..!!</Hyperlink>
</TextBlock>

Code behind

private void Hyperlink_Click(object sender, RoutedEventArgs e)
{
    System.Diagnostics.Process.Start("filePath");
}

Say file name is ABC.txt.

In case file is not copied to output path and is added in project as Resource, you can give relative path like this to open the file (assuming your file is added directly under the project):

System.Diagnostics.Process.Start("..\\..\\ABC.txt");

In case file is copied to output path, you can directly give file name since by default it will look for file in output path:

System.Diagnostics.Process.Start("ABC.txt");
like image 24
Rohit Vats Avatar answered Nov 15 '22 10:11

Rohit Vats