Skip to content

Instantly share code, notes, and snippets.

View RSa-Dev's full-sized avatar
🏠
Working from home

Roman RSa-Dev

🏠
Working from home
  • ex USSR
  • Moscow <=> Bishkek <=> Yerevan
View GitHub Profile
@RSa-Dev
RSa-Dev / Program.cs
Created September 1, 2024 19:08 — forked from emoacht/Program.cs
Execute command in .NET 5.0 and newer.
public static async Task<string[]> ExecuteCommandAsync(string command)
{
using var p = new Process();
p.StartInfo = new ProcessStartInfo("cmd.exe")
{
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardInput = true,
RedirectStandardOutput = true
};
@RSa-Dev
RSa-Dev / ProcessExtensions
Created September 1, 2024 19:04 — forked from jasontalon/ProcessExtensions
System.Diagnostics.Process Extensions
public static class ProcessExtensions
{
public static async Task<(string output, string error)> RunAsync(this Process process, string command,
CancellationToken cancellationToken = default)
{
if (string.IsNullOrEmpty(command)) throw new ArgumentNullException(nameof(command));
var outputBuilder = new StringBuilder();
var errorBuilder = new StringBuilder();
@RSa-Dev
RSa-Dev / Program.cs
Created September 1, 2024 18:58 — forked from HineshMandalia/Program.cs
Process Stream Read Async
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Threading.Channels;
using var p = new Process();
p.StartInfo.FileName = "ls";
await foreach(var s in p.StartAndReadOutputAsync()){
Console.WriteLine(s);
}
@RSa-Dev
RSa-Dev / ProcessAsyncHelper.cs
Created September 1, 2024 18:35 — forked from Indigo744/ProcessAsyncHelper.cs
The right way to run external process in .NET (async version)
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Threading.Tasks;
/// <summary>
/// Process helper with asynchronous interface
/// - Based on https://gist.github.com/georg-jung/3a8703946075d56423e418ea76212745
/// - And on https://stackoverflow.com/questions/470256/process-waitforexit-asynchronously
/// </summary>
@RSa-Dev
RSa-Dev / WebApiProgram.cs
Last active July 27, 2023 23:08
Simplest NET50 WebApi
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Linq;
using System;
@RSa-Dev
RSa-Dev / ReferToBackgroundService
Created July 27, 2023 22:20
Reference to Background Service
//regular implementation of Background/IHostedService. Nothing special.
public class MyBackgroundService : BackgroundService
{
}
//Startup.cs or Program.cs
//Also standard. No extra code required.
services.AddHostedService<MyBackgroundService>();
services.AddTransient<IMyService, MyService>();
@RSa-Dev
RSa-Dev / CopyFolder.cs
Created July 27, 2023 10:29
Copy of folder with all level files
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CopyFolder
{
class Program
@RSa-Dev
RSa-Dev / JWT-gen.cs
Last active July 27, 2023 09:22
JWT Token Generator
using Microsoft.IdentityModel.Tokens;
using App.Common.Interfaces; // for ITokenGenerator - obvious
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;
namespace Ordering.Infrastructure.Services
{
public class TokenGenerator : ITokenGenerator
{
@RSa-Dev
RSa-Dev / gist:3f2c1f8f2514c3162747457dfcd05360
Created July 26, 2023 11:08
MAUI Simple SQlite sample - NO EF
https://learn.microsoft.com/en-us/dotnet/maui/data-cloud/database-sqlite
https://github.com/dotnet/maui-samples/tree/main/7.0/Data/TodoSQLite
@RSa-Dev
RSa-Dev / IOptionMonitor.cs
Created June 30, 2023 00:01
IOptionMonitor in BackgroundService
// ...
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using System.Threading;
public class MySingletonService : BackgroundService
{
private IDisposable _optionsChangedListener;
private MyOptions _myCurrentOptions;