Does any one knows how we can find
When active directory was last backed up using C#?
according to my knowledge when we run this command
repadmin /showbackup
its will shows us full detail. I tried to get value of dsa signature using C#, but even that value does not make much sense, and will help us to get correct information of.
Like from which domain controller backup was initiated and on when?
Anyone knows how to get this last backup detail of active directory using C#?
Thanks in advance
I have found many times that not all may be done with WMI, The below code should run the requisite command to show the information you requested and then redirect it to standard out.
System.Diagnostics.ProcessStartInfo PSI =
new System.Diagnostics.ProcessStartInfo("cmd", "/c " + "Repadmin.exe /showbackup");
PSI.RedirectStandardOutput = true;
PSI.UseShellExecute = false;
PSI.CreateNoWindow = true;
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = PSI;
proc.Start();
string result = proc.StandardOutput.ReadToEnd();
Console.WriteLine(result);
}
catch (Exception e)
{
Messagebox.Show(e.InnerException);
}
}
Related
I'm new to c# as well as this website so I'll probably make some mistakes but hopefully I'll learn as well.
I'm trying to develop an app to extract from a .7z a file, read it and delete it before moving to the next (there is about 12k files in there, which take up a lot of space).
Now, I'm using .NET 2.0 and I haven't found easy solutions to extract single files from an archive. I came across a post explaining that you could use 7za.exe to do so from the command line, and so I did. Now, the problem is that if I try to do the same in my app 7za throws the "cannot use absolute pathnames" error. However, as I said, the same parameters work at the command prompt.
My idea is to get a list of all the files in the compressed folder and put it in a textfile. From there it is as easy as get the name of the first one, unzip it, read it, delete it and move to next. This is what I got to get the list:
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.FileName = #"C:\Users\yadayada\Desktop\7za.exe"; //just testing
string parameters= "-y l " + path_file7z + " > " + path_file_destination + #"\file.txt";
//needeless to say, path_file7z and path_file_destination are strings with the correct path like "C:\Users\yadayada\Desktop"
startInfo.Arguments = parameters
process.StartInfo = startInfo;
process.StartInfo.UseShellExecute = false;
process.Start();
process.WaitForExit();
As I said, as the process ends I can see a console opening and closing. I took a screenshot to check what it said and the error I got was the one I said at the begining of the question.
Now, does anyone know why I get this error and how can I fix this?
SOLUTION
Ended un going for a not too elegant (being gentle) solution that works.
To list the names:
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.FileName = #"cmd.exe";
string parameters= "/k -y l " + path_file7z + " > " + path_file_destination + #"\file.txt";
//needeless to say, path_file7z and path_file_destination are strings with the correct path like "C:\Users\yadayada\..."
startInfo.Arguments = parameters
process.StartInfo = startInfo;
process.StartInfo.UseShellExecute = false;
process.Start();
Thread.Sleep(1000);
process.Kill();
Now, once this has been done the extract part is tanken care of by a method similar to the one provided by Igor's links:
public void ExtractFile(string source, string element, string destination)
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = /*7zG.exe's path*/;
startInfo.Arguments = #"-y x " + source + " -o" + destination + " " + element + " -r";
process.StartInfo = startInfo;
process.StartInfo.UseShellExecute = false;
process.Start();
process.WaitForExit();
}
I think it is working well now. If I have further issues I'll update this with the solutions I come up with with the help provided. Thanks again!
Use this tutorial for 7zip:
https://stick2basic.wordpress.com/2013/04/25/how-to-extract-7z-file-to-a-folder-c-net/
Use this library if rar and zip are allowed:
http://dotnetzip.codeplex.com/
private void MyExtract()
{
string zipToUnpack = "C1P3SML.zip";
string unpackDirectory = "Extracted Files";
using (ZipFile zip1 = ZipFile.Read(zipToUnpack))
{
// here, we extract every entry, but we could extract conditionally
// based on entry name, size, date, checkbox status, etc.
foreach (ZipEntry e in zip1)
{
e.Extract(unpackDirectory, ExtractExistingFileAction.OverwriteSilently);
}
}
}
EDIT
This can help you:
Unzip a file in c# using 7z.exe
Happy to help you!
I'm trying to create a ASP.NET web api to trigger a crawl event to happen. I can't seem to get cygwin to process any of the commands I give it. The only thing I can really do is get it to open a terminal. Once the terminal is open I'd have to redirect the pwd to another location and then trigger my command I want.
Process p = new Process();
ProcessStartInfo info = new ProcessStartInfo();
info.CreateNoWindow = false;
info.RedirectStandardInput = true;
info.UseShellExecute = false;
info.FileName = "C:\\cygwin64\\bin\\mintty.exe";
p.StartInfo = info;
p.Start();
StreamWriter sw = p.StandardInput;
if (sw.BaseStream.CanWrite)
{
sw.WriteLine(#"cd C:\Users\UName\Desktop\apache-nutch-2.3-mongodb\runtime\local\");
sw.WriteLine("bin/autoCrawl");
}
sw.Close();
p.WaitForExit();
I've tried many approaches, this is the last one I've tried but it just does nothing. Is there a way to launch this crawl from my .NET application? I've looked into the NutchApi about creating a new job with a type of crawl but I'm not sure if that applies here or not.
I ended up figuring out how to use the NutchApi to answer my question.
I'm setting local auditing policies from a C# .NET program that reads settings from a file then uses Process.Start() with 'cmd' to execute the commands. This way has worked in the past for everything that I've needed it to do (including this exact situation), but recently it's just started to mysteriously fail to set the policies.
Here's the code: (command is of the form "auditpol /set /subcategory:"blah" /success:enable")
ProcessStartInfo procStartInfo = new ProcessStartInfo("cmd", "/c " + command);
procStartInfo.RedirectStandardOutput = true;
procStartInfo.RedirectStandardError = true;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = true;
Process proc = new Process();
proc.StartInfo = procStartInfo;
proc.Start();
proc.WaitForExit();
string result = proc.StandardOutput.ReadToEnd();
string error = proc.StandardError.ReadToEnd();
In debug in VS2013 it's applying the policies just fine and even on the same computer in the full on .exe it's applying just fine, but when it gets transferred to another computer it will not set the policies from the auditpol command. Anyone have any ideas what could be happening?
The specific problem I am seeing when executing a cmd process with something like "del *.txt" where one of the 'txt' files is open and cannot be deleted, the cmd process will output a line of text (saying something like 'file in use, cannot delete file') to the console, but not to the StandardOutput or the StandardError. According to this question [ https://stackoverflow.com/a/320779/832705 ] from 2008, the answer is no, but I am wondering if that might have changed in the past 4 years, or if someone has since found a workaround way. Also, I might be misinterpreting that answer, it might mean CLR exceptions and not cmd exceptions.
here is my process setup/start code:
ProcessStartInfo psi = new ProcessStartInfo("cmd", string.Empty);
psi.CreateNoWindow = true;
psi.ErrorDialog = false;
psi.RedirectStandardError = true;
psi.RedirectStandardOutput = true;
psi.RedirectStandardInput = true;
psi.UseShellExecute = false;
Process p = new Process();
p.StartInfo = psi;
p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);
p.ErrorDataReceived += new DataReceivedEventHandler(p_ErrorDataReceived);
outputfilesw = new StreamWriter(outputfile, true);
try
{
p.Start();
p.BeginOutputReadLine();
//work code
}
You just have to call p.BeginErrorReadLine() to start the asynchronous read of StandardError. Answer added at suggestion of OP.
You can read the output, and you can process the text returned. So, you should be able to find the text that indicates an error, even if it doesn't land in the error output.
Also, it is important to note that only the process being run can determine which output stream gets a message. So, if the command you're using decides to send errors to the standard stream, no amount of OS or C# work will change that.
I'm coding a watchdog service for an embedded system that monitors some proprietary processes and restarts them if necessary. (Nothing to do with malware, before you ask. It's just a business requirement)
I need to retrieve the friendly name from a process I have just created, so that later I can retrieve that process using that name in order to monitor its health.
My problem is as follows:
If I try to read Process.ProcessName right after Process.Start(), I get an InvalidOperationException because it the process has not been fully created yet.
The same happens if I use Process.WaitForInputIdle(), but since this requires a message pump and many executables could be UI-less launchers for the actual application, this might not be an option.
I need to get the friendly name right after creating the process, before doing anything else.
Here's a code snippet:
var startInfo = new ProcessStartInfo { FileName = "notepad.exe" };
var process = new Process();
process.StartInfo = startInfo;
process.Start();
process.WaitForInputIdle();
var friendlyName = process.ProcessName;
This will throw an InvalidOperationException on the last line if the process being started is Firefox, for example.
So how would I do this? Is there a safer method?
EDIT: Added a code snippet for clarification.
EDIT2: Rephrased the whole question for clarification, left irrelevant stuff out.
Ok, so after a lot of research I had to resort to a somewhat hacky solution.
According to the Process.GetProcessesByName() documentation, "The process name is a friendly name for the process, such as Outlook, that does not include the .exe extension or the path.".
Considering this, I worked around the problem using this code:
var startInfo = new ProcessStartInfo { FileName = "notepad.exe" };
var process = new Process();
process.StartInfo = startInfo;
process.Start();
var friendlyName = Path.GetFileNameWithoutExtension(startInfo.FileName);
As I said, it still feels kinda hacky, but at least it got the job done and allowed me to move on.
Thanks for all your comments anyway!
You are missing some key things in your code and try something like this using NotePad.exe and you will see what I am talking about
var startInfo = new ProcessStartInfo { FileName = "notepad.exe" };
var process = new Process();
process.StartInfo = startInfo;
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = false;
process.Start();
process.WaitForInputIdle();
// this line below will always be false because `process.ProcessName will be
//notepad and process.StartInfo.FileName = notepade.exe
if (process.ProcessName.Equals(process.StartInfo.FileName) == false)
{
var theConfiguredProcessName = process.ProcessName;
}
Another option you could do either of the next 2 things below
Process[] processName = Process.GetProcessesByName("blah.exe");
or check all processes running and check for your running process
Process[] processlist = Process.GetProcesses();
foreach(Process process in processlist)
{
Console.WriteLine("Process: {0} ID: {1}", process.ProcessName, process.Id);
}