我有一個控制檯應用程序和一個Winforms應用程序也是這樣做的。通用功能是在一個類中被兩者重用。爲什麼批處理文件在從Windows Forms應用程序調用時不能複製文件,但它可以從控制檯應用程序中運行?
CopyRequiredFile啓動一個windows批處理文件,它使用xcopy將文件從網絡文件夾複製到本地驅動器。但是,從Windows窗體應用程序調用時,它不會複製文件。
我是新手開發者試圖開發框架和UI自動化的一些內部工具。
爲什麼在我從控制檯應用程序調用功能時複製文件,而不是從Windows窗體應用程序中複製文件?
我的控制檯應用程序:
public class Program
{
private static readonly Action<string> OutputAction = s => Console.WriteLine(s);
private static readonly IProgress<string> Progress = new Progress<string>(OutputAction);
public static void Main(string[] args)
{
HelpersCopy.CreateRequiredDirectories(Progress);
HelpersCopy.CopyRequiredFiles(Progress, true);
HelpersCopy.StartHub(Progress);
HelpersCopy.StartNode(Progress);
Console.WriteLine("Press any key to continue...");
Console.ReadKey();
}
}
我的Windows窗體應用程序:只有是有關這個問題的代碼。
private void button1_Click(object sender, EventArgs e)
{
Action<string> outputAction = s => txtOutput.InvokeEx(t => t.Text += s + Environment.NewLine);
IProgress<string> progress = new Progress<string>(outputAction);
txtOutput.Clear();
HelpersCopy.CreateRequiredDirectories(progress);
HelpersCopy.CopyRequiredFiles(progress, true);
HelpersCopy.StartHub(progress);
HelpersCopy.StartNode(progress);
}
InvokeEx是一個擴展方法來調用動作,如果需要的話。從stackoverflow幫助!
不幸的是,我無法發佈圖片,因爲我沒有要求的要點。因此,請在這裏看到的輸出圖像:https://www.flickr.com/photos/[email protected]/sets/72157649781440604/
助手類代碼請讓我知道,如果在這個問題並不需要此代碼。
public class HelpersCopy
{
public static void CopyRequiredFiles(IProgress<string> progress, bool hideWindow = false)
{
progress.Report(string.Format("Copying latest version of executables...{0}", Environment.NewLine));
var currentDirectory = Directory.GetCurrentDirectory();
ExecuteCommand(String.Format(@"{0}\Copy latest Selenium files.bat", currentDirectory), progress, hideWindow: hideWindow);
progress.Report(string.Format("\r\nLatest version of executables copied successfully{0}", Environment.NewLine));
}
private static void ExecuteCommand(string fileName, IProgress<string> progress, string command = null, bool hideWindow = true)
{
if (hideWindow)
{
var processInfo = new ProcessStartInfo(fileName, command)
{
CreateNoWindow = true,
UseShellExecute = false,
// *** Redirect the output ***
RedirectStandardError = true,
RedirectStandardOutput = true
};
var process = new Process { StartInfo = processInfo, EnableRaisingEvents = true };
process.ErrorDataReceived += (sender, args) => progress.Report(args.Data);
process.OutputDataReceived += (sender, args) => progress.Report(args.Data);
var started = process.Start();
progress.Report(string.Format("process started: {0}", started));
progress.Report(string.Format("process id: {0}", process.Id));
progress.Report(string.Format("process start info: {0} {1}", process.StartInfo.FileName, process.StartInfo.UserName));
process.BeginErrorReadLine();
process.BeginOutputReadLine();
process.WaitForExit();
int ExitCode = process.ExitCode;
progress.Report(string.Format("ExitCode: {0}{1}", ExitCode, Environment.NewLine));
process.Close();
}
else
{
var process = Process.Start(fileName, command);
if (process.HasExited)
{
throw new Exception(string.Format("Process exited. Exit code: {0}", process.ExitCode));
}
}
}
}
我的批處理文件
@echo off
echo Deleting existing mappings...
net use z: /delete /yes
echo Mapping network drives...
net use z: \\company-filestore\Selenium /user:company-filestore\Automation Selen1um
z:
cd "Hub and Node Executables"
echo Copying latest Selenium jars...
xcopy "z:\Hub and Node Executables" "C:\Selenium\" /R /Y /S /Z
echo Finished copying latest Selenium jars...
echo All done
請問您可以將這個問題稍微壓縮一下,以強調重要的部分?有很多噪音 – DLeh
當然@DLeh。我會嘗試刪除不必要的代碼,並適當地聲明所需的行爲。 – raluru
我試着重寫你的問題的陳述,所以它更清楚,希望這有助於。 – Dzyann