Tob*_*ayn 5 .net c# linux .net-core .net-6.0
以下场景:
现在我想编写一个可以更改用户密码的 C# .NET 6 应用程序。在Linux中我只会调用
sudo passwd username
Run Code Online (Sandbox Code Playgroud)
我的第一个想法是创建一个新进程,重定向标准输入并在那里写入密码。
另一种选择是创建一个 bash 脚本并从 C# 调用它(也使用新进程)。
然而,两者对我来说似乎都有点老套。C# 中有没有可以在进程中更改密码的选项?
如果您已经有了 Linux shell 脚本,那么这些内容应该可以正常工作。
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)){
var ShellScriptPath = $"-c '{ShellScriptPath } &>> {OutputTextFilePath}'";
var fileInfo = new FileInfo(ShellScriptPath);
var startInfo = new ProcessStartInfo
{
CreateNoWindow = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
FileName = "/bin/bash",
WindowStyle = ProcessWindowStyle.Hidden,
Arguments = fileInfo.FullName,
WorkingDirectory = WorkingDirectoryPath,
};
using Process process = Process.Start(startInfo);
process.WaitForExit();
process.Dispose();
}
Run Code Online (Sandbox Code Playgroud)