How can I create a link in XAML to open a text file? The text file directory is same as the project directory.
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.
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.
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");
}
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");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With