处理类以使用文件共享启动bat文件 [英] Process class to start bat files using fileshares

查看:85
本文介绍了处理类以使用文件共享启动bat文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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.

推荐答案

您是否尝试过将任何变量作为参数传递到bat脚本中?
这样就不必更改脚本,只需更改StartInfo.

另外,如果可以配置安全性,是否可以使用UNC路径而不是映射驱动器?

尼克
Have you tried passing any variables into the bat script as parameters?

Then you would not have to changed the script, just the StartInfo.

Also, if you can configure the security, could you use UNC paths instead of mapping a drive?

Nick


您可以在代码中添加以下内容;

You can put the following in your code;

if(Directory.Exists("s:/") == false)
{
  Process.Start("net", "use s: \\UNCPATH PASSWORD /user:DOMAIN\USERNAME")
}


我粘贴的代码是一个较大程序的一部分,该程序意味着
启动任何.exe或.bat文件.我没有知道蝙蝠文件要做什么的奢侈.它必须是一个通用的解决方案.

Rod,执行Directory.Exists("s:/" == false)是不可行的选择.

尼古拉斯,您可以扩展提到的UNC路径解决方案吗?我假设您的意思是"//服务器名/路径名",后跟用户名/密码"组合.我猜该程序可以有一个配置文件来指定用户名/密码,但是如果它使用共享给可能具有不同登录名的不同服务器的共享怎么办?另外,我应该在StartInfo中指定所有这些内容吗?它是域"属性,还是用户名和密码"属性?

基本上,我想为正在启动的PROCESS对象提供足够的特权,以便它的工作方式与用户登录时启动它的方式完全相同.

谢谢与问候

柴.
The code that I have pasted is part of a bigger program which is meant
to start ANY .exe''s or .bat files. I don''t have the luxury of knowing what the bat file is going to do. It must be a general solution.

Rod, doing a Directory.Exists("s:/" == false) is not a viable option.

Nicholas, can you expand on the UNC paths solution that you have mentioned ? I assume you mean "//servername/pathname" followed by the User/password combination. I guess that the program can have a config file to specify the user/password, but then what if it uses shares to different servers which may have different logins ? Also, where would I specify all this in StartInfo ? Would it be the "Domain" attribute along with the "Username & Password" attributes ?

Basically, what I want to do is supply enough privileges to the PROCESS object being started, so that it works exactly the same way as if the user were starting it when logged in.

Thanks & regards

Chai.


这篇关于处理类以使用文件共享启动bat文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆