खोज…


स्टॉपवॉच देखनी

यह उदाहरण दिखाता है कि Stopwatch का उपयोग कोड के ब्लॉक को बेंचमार्क करने के लिए कैसे किया जा सकता है।

using System;            
using System.Diagnostics;
        
public class Benchmark : IDisposable
{
    private Stopwatch sw;

    public Benchmark()
    {
        sw = Stopwatch.StartNew();
    }

    public void Dispose()
    {
        sw.Stop();
        Console.WriteLine(sw.Elapsed);
    }
}

public class Program
{
    public static void Main()
    {
        using (var bench = new Benchmark())
        {
            Console.WriteLine("Hello World");
        }
    }
}

शेल कमांड चलाएं

string strCmdText = "/C copy /b Image1.jpg + Archive.rar Image2.jpg";
System.Diagnostics.Process.Start("CMD.exe",strCmdText);

यह cmd विंडो को छिपाना है।

System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C copy /b Image1.jpg + Archive.rar Image2.jpg";
process.StartInfo = startInfo;
process.Start();

सीएमडी और आउटपुट प्राप्त करने के लिए कमांड भेजें

यह विधि command को Cmd.exe भेजने की अनुमति देता है, और मानक आउटपुट (मानक त्रुटि सहित) को एक स्ट्रिंग के रूप में लौटाता है:

private static string SendCommand(string command)
{
    var cmdOut = string.Empty;
    
    var startInfo = new ProcessStartInfo("cmd", command)
    {
        WorkingDirectory = @"C:\Windows\System32", // Directory to make the call from
        WindowStyle = ProcessWindowStyle.Hidden,   // Hide the window
        UseShellExecute = false,                   // Do not use the OS shell to start the process
        CreateNoWindow = true,                     // Start the process in a new window 
        RedirectStandardOutput = true,             // This is required to get STDOUT
        RedirectStandardError = true               // This is required to get STDERR
    };

    var p = new Process {StartInfo = startInfo};

    p.Start();

    p.OutputDataReceived += (x, y) => cmdOut += y.Data;
    p.ErrorDataReceived += (x, y) => cmdOut += y.Data;
    p.BeginOutputReadLine();
    p.BeginErrorReadLine();
    p.WaitForExit();
    return cmdOut;
}

प्रयोग

var servername = "SVR-01.domain.co.za";
var currentUsers = SendCommand($"/C QUERY USER /SERVER:{servername}")

उत्पादन

string currentUsers = "USERNAME SESSIONNAME ID STATE IDLE TIME LOGON TIME जो।। 2016 08:33 Andy.McAnderson ica-cgp # 2 4 Active। 25/07/2016 08:54 John.Smith ica-cgp # 4 5 सक्रिय 14 25/07/2016 08:57 बॉब। बॉबिंगटन ica-cgp # 5 6 सक्रिय 24692 + 13: 29 25/07/2016 09:05 टिम.ओम आईसीआईसी-सीजीपी # 6 7 ... 25/07/2016 09:08 बॉब। जोग्स ica-सीजीपी # 7 8 सक्रिय 24692 + 13: 29 25 / 07/2016 09:13 "


कुछ अवसरों पर, प्रश्न में सर्वर तक पहुंच कुछ उपयोगकर्ताओं के लिए प्रतिबंधित हो सकती है। यदि आपके पास इस उपयोगकर्ता के लिए लॉगिन क्रेडेंशियल हैं, तो इस पद्धति से प्रश्न भेजना संभव है:

private static string SendCommand(string command)
{
    var cmdOut = string.Empty;
    
    var startInfo = new ProcessStartInfo("cmd", command)
    {
        WorkingDirectory = @"C:\Windows\System32",
        WindowStyle = ProcessWindowStyle.Hidden,    // This does not actually work in conjunction with "runas" - the console window will still appear!
        UseShellExecute = false,
        CreateNoWindow = true,
        RedirectStandardOutput = true, 
        RedirectStandardError = true,

        Verb = "runas",
        Domain = "doman1.co.za",
        UserName = "administrator",
        Password = GetPassword()
    };

    var p = new Process {StartInfo = startInfo};

    p.Start();

    p.OutputDataReceived += (x, y) => cmdOut += y.Data;
    p.ErrorDataReceived += (x, y) => cmdOut += y.Data;
    p.BeginOutputReadLine();
    p.BeginErrorReadLine();
    p.WaitForExit();
    return cmdOut;
}

पासवर्ड प्राप्त करना:

static SecureString GetPassword()
{
    var plainText = "password123";
    var ss = new SecureString();
    foreach (char c in plainText)
    {
        ss.AppendChar(c);
    }

    return ss;
}

टिप्पणियाँ

उपरोक्त दोनों विधियाँ STDOUT और STDERR का एक OutputDataReceived , क्योंकि OutputDataReceived और ErrorDataReceived दोनों एक ही चर - cmdOut



Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow