how can change system date time in ubuntu os with .net core

Viewed 1370

I tried to Change system date time in Linux based operating system, but in my search, I could't find any proper help, Now, my Question is How can I change system date time with .net core2.2 in ubuntu OS?

1 Answers

A quick google search brings up a nice helper method for doing this...

using System;
using System.Diagnostics;

public static class ShellHelper
{
    public static string BashCommand(string cmd)
    {
        var escapedArgs = cmd.Replace("\"", "\\\"");

        var process = new Process()
        {
            StartInfo = new ProcessStartInfo
            {
                FileName = "/bin/bash",
                Arguments = $"-c \"{escapedArgs}\"",
                RedirectStandardOutput = true,
                UseShellExecute = false,
                CreateNoWindow = true,
            }
        };
        process.Start();
        string result = process.StandardOutput.ReadToEnd();
        process.WaitForExit();
        return result;
    }
}

Source: https://loune.net/2017/06/running-shell-bash-commands-in-net-core/

then give it a call

string cmd = ""; // see below
ShellHelper.BashCommand(cmd);

You can get the bash command to change the date here...
https://www.garron.me/en/linux/set-time-date-timezone-ntp-linux-shell-gnome-command-line.html

Note that this obviously won't work on a different operating system, so it is not cross-platform code. Make sure you handle these cases accordingly.

Related