C# .Net 6 Samples

Some example of simple .Net 6 but common coding scenarios (CS# lang v10)

Could also be converted to a JSON Serializer or XML reader

Parse each line from an auto genereted web hosted text file containing version info in order to perform self update.

Async File Download using HttpClient

{
    string url = Properties.Settings.Default.UpdateHistoryUrl;
    List<;ApplicationVersion>; versions;
    using (HttpClient client = new HttpClient())
    {
        var response = await client.GetStreamAsync(url);
        var streamReader = new StreamReader(response);
        versions = ParseFromUpdateTextFile(streamReader);
        versions.Sort();
    }

    return versions.Count >; 0 ? versions.Last() : null;
}

Open any file using default application by UseShellExecute

    public static bool OpenImageInDefaultAplication([NotNull] string fileName)
{
    try
    {
        if (!File.Exists(fileName)) throw new ArgumentException("File does not exist", nameof(fileName));
        ProcessStartInfo psi = new ProcessStartInfo(fileName) { UseShellExecute = true };
        Process.Start(psi);
        return true;

    }
    catch (Exception ex)
    {
        Log.Error(ex, "OpenImageInDefaultAplication: {Message}", ex.Message);
        return false;
    }
}

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.