问题描述
我有一个C#程序(作为服务或Windows应用程序运行),它在Windows计算机上启动.exe或.bat文件-它使用PROCESS类来执行此操作.我还从中捕获标准输出-所有这些工作都令我满意.
当我运行bat文件(它将文件从共享文件传输到本地d驱动器)时,以登录用户的身份,只需通过Windows资源管理器双击该文件,即可正常运行.但是,如果我从程序中启动了相同的bat文件,则bat文件不会执行它应该执行的操作. bat文件可以运行,但无法成功传输文件.
我已经查看了StartInfo的所有可能属性,但均未成功.通过更改bat文件以包含"NET USE"命令来映射驱动器,我已经取得了成功-但这需要更改bat文件,而我可能并不总是这样做.我希望bat文件按原样运行.
bat文件的内容为:
cd C:\
复制s:\ eftsource \ cards041210 d:\ eftrans \ outgoing
我的C#程序中的代码行是:
I have a C# program (which runs either as a service or a windows app) that starts either .exe''s or .bat files on a Windows machine - it uses the PROCESS class to do this. I also capture standard output from it - all of this works to my satisfaction.
When I run the bat file (it transfers a file from a share to the local d-drive), as a logged on user by simply double-clicking on it via Windows Explorer, it works fine. However, if I start this same bat file from my program, the bat file does not do what it is supposed to do. The bat file does run but is not successful in transferring the file.
I have looked at all possible attributes for StartInfo with no success. I have had success by changing the bat file to include a "NET USE " command to map the drive - but that requires changing the bat files which I may not always have the luxury to do. I want the bat file to run as is.
The bat file contents are:
cd C:\
copy s:\eftsource\cards041210 d:\eftrans\outgoing
The lines of code from my C# program are:
// Current Process
currentProcess = new Process();
currentProcess.StartInfo.FileName = strJobFilename;
currentProcess.StartInfo.Arguments = strJobArguments;
currentProcess.StartInfo.WorkingDirectory =
Path.GetDirectoryName(strJobFilename);
strExtension = Path.GetExtension(strJobFilename);
currentProcess.StartInfo.
currentProcess.StartInfo.UseShellExecute = false;
// Redirect Standard Output
currentProcess.StartInfo.RedirectStandardOutput = true;
currentProcess.OutputDataReceived +=
new DataReceivedEventHandler(StandardOutputHandler);
// Redirect Standard Error
currentProcess.StartInfo.RedirectStandardError = true;
currentProcess.ErrorDataReceived +=
new DataReceivedEventHandler(StandardErrorHandler);
// Other StartInfo properties
currentProcess.StartInfo.CreateNoWindow = true;
try
{
dtBeforeStartTime = DateTime.Now;
bProcessStarted = currentProcess.Start();
}
问候
Chai.
Regards
Chai.
推荐答案
if(Directory.Exists("s:/") == false)
{
Process.Start("net", "use s: \\UNCPATH PASSWORD /user:DOMAIN\USERNAME")
}
这篇关于处理类以使用文件共享启动bat文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!