成功复制后关闭Powershell。 [英] Closing Powershell After Copying Successfully.
问题描述
我正在尝试执行powershell,将一些文件从一台服务器复制到另一台服务器。
我有使用C#中的system.diagnostics.process执行copy-item命令。但是,当复制文件完成Powershell仍然打开时。我必须从任务管理器手动结束Powershell。
对于每个文件,生成一个单独的PowerShell进程,所以我想在成功复制后结束每个PowerShell进程。 Plz告诉我该怎么做。
我用Powershell复制文件的方法
Hi,
I am trying to execute powershell to copy some files from one server to another in .Net.
I have executed copy-item command using system.diagnostics.process from the C#. But When the Copying of files completes the Powershell Remains Open. and I have to End the Powershell Manually from Task Manager.
For Each and every files a separate powershell process is generating so i want to end every powershell process after successfully copying. Plz tell me how to do that.
My Method to Copy the files with Powershell
private static void copy(string source,string dest)
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "powershell.exe";
//dest = @"D$\";
dest = dest.Remove(1, 1).Insert(1, "$");
dest = dest + "\\";
string server = "10.0.4.42";
string xx = "copy-item -path " + source + " \\\\" + server + "\\" + dest + " -Recurse -Force";
startInfo.Arguments = xx;
//string ex = "exit";
//startInfo.Arguments = ex;
startInfo.CreateNoWindow = true;
startInfo.RedirectStandardInput = true;
startInfo.RedirectStandardOutput = true;
startInfo.UseShellExecute = false;
process.StartInfo = startInfo;
process.Start();
推荐答案
\;
dest = dest。删除( 1 , 1 )。插入( 1 ,
\"; dest = dest.Remove(1, 1).Insert(1, "
);
dest = dest + \\;
string server = 10.0.4.42;
string xx = copy-item -path + source + \\\\ + server + \\ + dest + -Recurse -Force;
startInfo.Arguments = xx;
// string ex =exit;
// startInfo.Arguments = ex;
startInfo.CreateNoWindow = true 跨度>;
startInfo.RedirectStandardInput = true ;
startInfo.RedirectStandardOutput = true ;
startInfo.UseShellExecute = false ;
process.StartInfo = startInfo;
process.Start();
"); dest = dest + "\\"; string server = "10.0.4.42"; string xx = "copy-item -path " + source + " \\\\" + server + "\\" + dest + " -Recurse -Force"; startInfo.Arguments = xx; //string ex = "exit"; //startInfo.Arguments = ex; startInfo.CreateNoWindow = true; startInfo.RedirectStandardInput = true; startInfo.RedirectStandardOutput = true; startInfo.UseShellExecute = false; process.StartInfo = startInfo; process.Start();
这篇关于成功复制后关闭Powershell。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!