简体   繁体   English

在C#中执行具有路径依赖性的批处理文件

[英]Executing Batch file which has path dependency in c#

I have a bat file, which copies files from one directory to other. 我有一个bat文件,它将文件从一个目录复制到另一个目录。 If this bat file is clicked manually, files are copied succesfully and no issues with that. 如果手动单击此bat文件,将成功复制文件,并且没有问题。 But if bat file is executed through C#, "files not found" message is displayed in the cmd window. 但是,如果通过C#执行bat文件,则cmd窗口中将显示“找不到文件”消息。

Here is my bat file. 这是我的蝙蝠文件。

echo off 
echo. 
XCOPY "..\SourceFolder\CaSourceFile" "..\DestinationFolder\SubFolder" /r /Y /i /F

if full path is given in bat file, then files are copied successfully. 如果bat文件中提供了完整路径,则文件复制成功。 example, 例,

XCOPY "D:\RootFolder\SourceFolder\CaSourceFile" "D:\RootFolder\DestinationFolder\SubFolder" /r /Y /i /F

My C# code: 我的C#代码:

ProcessStartInfo processInfo = new ProcessStartInfo(batchFile);
                processInfo.UseShellExecute = true;
                Process batchProcess = new Process();
                batchProcess.StartInfo.FileName = "cmd.exe";
                batchProcess.StartInfo = processInfo;
                batchProcess.StartInfo.Arguments = String.Format("\"{0}\" \"{1}\"", @"D:\Europa\Test Release Tool\SingleExeInstaller\EuropaApplication", @"D:\Europa\Test Release Tool\SingleExeInstaller\EuropaInstaller");
                batchProcess.StartInfo.RedirectStandardInput = true;
                batchProcess.StartInfo.RedirectStandardOutput = true;
                batchProcess.StartInfo.RedirectStandardError = true;
                batchProcess.Start();
                batchProcess.WaitForExit();

My application exe is available in C: and bat file is available in D: Is there any idea to over come this issue ? 我的应用程序exe在C中可用,而bat文件在D中可用:是否有解决此问题的想法? Kindly help me out. 请帮我。

You need to set the WorkingDirectory on the StartInfo ; 您需要在StartInfo上设置WorkingDirectory your relative paths in the batch file are off because of that. 因此,批处理文件中的相对路径已关闭。 This should be the path to the batch file likely. 这应该是批处理文件的路径

batchProcess.StartInfo.WorkingDirectory = Path.GetDirectoryName(batchFile);

The paths you are trying to use are Relative paths, the question is what are they relative to. 您尝试使用的路径是相对路径,问题是它们相对于什么。

When you run the bat file from the command line they will be relative to the current directory which is what you set using the cd command and is likely the directory your .bat file resides in. When you run the bat from your C# exe file the current directory will be the directory your exe file is in. 当您从命令行运行bat文件时,它们将相对于您使用cd命令设置的当前目录,并且可能是您的.bat文件所在的目录。当您从C#exe文件运行bat时,当前目录将是您的exe文件所在的目录。

The current directory is also known as the WorkingDirectory. 当前目录也称为WorkingDirectory。 In order to use a different directory try setting the WorkingDirectory property on the ProcessStartInfo object. 为了使用其他目录,请尝试在ProcessStartInfo对象上设置WorkingDirectory属性。

我希望您需要在StartInfo中设置工作目录。请参见.NET Process.Start默认目录?

It's easier to copy the files from one folder to another via C#. 通过C#将文件从一个文件夹复制到另一个文件夹更容易。

Easy example: 简单的例子:

using System.IO;

string sourcePath = "C:\test";
string targetPath = "D:\test_new";
if (!Directory.Exists(targetPath)) {
Directory.CreateDirectory(targetPath);
}
foreach (var srcPath in Directory.GetFiles(sourcePath)) {
//Copy the file from sourcepath and place into mentioned target path, 
//Overwrite the file if same file is exist in target path
File.Copy(srcPath, srcPath.Replace(sourcePath, targetPath), true);
}

More details about file.copy you can find on MSDN as usual ))) - http://msdn.microsoft.com/en-us/library/system.io.file.copy.aspx 您可以像往常一样在MSDN上找到有关file.copy的更多详细信息)))-http: //msdn.microsoft.com/zh-cn/library/system.io.file.copy.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM