WPF - DataGridHyperlinkColumn not opening local HTML file

0

Environment: VS2019 - ver 16.4.2, Windows 10 - Pro ver 1903, WPF Core 3.1

Why the following code is not opening the file from the link in DataGridHyperlinkColumn in my WPF app. According to this post it should open the file - unless I'm missing something here.

XAML

<Window x:Class="WPF_DataGrid_EFCore.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WPF_DataGrid_EFCore"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <DataGrid x:Name="dgTest" AutoGenerateColumns="False" Margin="0,43,0,0">
            <DataGrid.Columns>
                <DataGridHyperlinkColumn Header="Title" Binding="{Binding Title}">
                    <DataGridHyperlinkColumn.ElementStyle>
                        <Style TargetType="TextBlock">
                            <EventSetter Event="Hyperlink.Click" Handler="dgTest_HyperLink_Click"/>
                        </Style>
                    </DataGridHyperlinkColumn.ElementStyle>
                </DataGridHyperlinkColumn>
            </DataGrid.Columns>
        </DataGrid>
        <Button x:Name="btnTest" Content="Test" HorizontalAlignment="Left" Margin="355,6,0,0" VerticalAlignment="Top" Click="btnTest_Click"/>
    </Grid>
</Window>

MainWindow.Xaml.cs

private void dgTest_HyperLink_Click(object sender, RoutedEventArgs e)
{
    Hyperlink link = (Hyperlink)e.OriginalSource;
    Console.WriteLine(link.NavigateUri.AbsoluteUri); //correctly shows the path as file:///C:/Users/MyUserName/AppData/Local/Temp/TestLocalFile.htm
    System.Diagnostics.Process.Start(link.NavigateUri.AbsoluteUri);
}

Remark

  1. The file is there and I can manually open it by pasting the following link directly to the Windows Explorer: file:///C:/Users/MyUserName/AppData/Local/Temp/TestLocalFile.htm. It opens in my default browser Google Chrome.
  2. According to this post the file should open.

Error

System.ComponentModel.Win32Exception HResult=0x80004005 Message=The system cannot find the file specified. Source=System.Diagnostics.Process .... .....

c#
wpf
.net-core
asked on Stack Overflow Dec 25, 2019 by nam • edited Apr 23, 2020 by Pavel Anikhouski

1 Answer

1

You should update the code, which is launching link per follows

private void dgTest_HyperLink_Click(object sender, RoutedEventArgs e)
{
    Hyperlink link = (Hyperlink)e.OriginalSource;
    Console.WriteLine(link.NavigateUri.AbsoluteUri); //correctly shows the path as file:///C:/Users/MyUserName/AppData/Local/Temp/TestLocalFile.htm

    var psi = new ProcessStartInfo
    {
        FileName = link.NavigateUri.AbsoluteUri,
        UseShellExecute = true
    };
    Process.Start(psi);
}

UseShellExecute does the magic here, in .NET Core it's set to false by default and

When UseShellExecute is false, the FileName property can be either a fully qualified path to the executable, or a simple executable name that the system will attempt to find within folders specified by the PATH environment variable.

In your case file name isn't executable, so for make it working you have to set UseShellExecute to true, this is correct for .NET Core apps.

The attached answer is 8 years old and using the classic .NET, where this property is true by default. You may also refer to this GitHub thread for details

answered on Stack Overflow Dec 25, 2019 by Pavel Anikhouski • edited Apr 19, 2020 by Pavel Anikhouski

User contributions licensed under CC BY-SA 3.0