设置ProcessStartInfo.WorkingDirectory一个UNC路径
-
24-09-2019 - |
题
我有一个工具,我已经写在VB.net是运行的计划任务。它在内部调用另一个可执行文件,它具有访问映射驱动器。显然,窗户与访问映射驱动器时,不登录的用户,即使在认证证书被提供给任务本身的计划任务的问题。好了,细
要解决这个问题我刚刚通过我的应用程序的UNC路径。
process.StartInfo.FileName = 'name of executable'
process.StartInfo.WorkingDirectory = '\\unc path\'
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
process.StartInfo.Arguments = 'arguments to executable'
process.Start()
这是我与映射的驱动器中使用相同的实现,但是使用UNC路径,该处理不表现得好像UNC路径是工作目录。
是否有任何已知问题设置ProcessStartInfo.WorkingDirectory一个UNC路径?如果不是这样,我在做什么错了?
解决方案
您使用映射驱动器的问题时,在没有登录用户的是,它们不存在。驱动器只映射,并提供给用户当前登录。如果没有一个已登录,没有驱动器映射。
作为一种变通方法,您可以通过CMD运行,呼叫PUSHD将您的UNC映射到幕后驱动器,然后执行代码。我已经从我的SYSTEM32复制的tree.com文件,并把它放在我的文件服务器上的“tree4.com”这个代码按预期工作(我也重定向标准输出,所以我可以看到调用的结果,但是公司不是必要的)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Using P As New Process()
'Launch a standard hidden command window
P.StartInfo.FileName = "cmd.exe"
P.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
P.StartInfo.CreateNoWindow = True
'Needed to redirect standard error/output/input
P.StartInfo.UseShellExecute = False
P.StartInfo.RedirectStandardInput = True
P.StartInfo.RedirectStandardOutput = True
'Add handler for when data is received
AddHandler P.OutputDataReceived, AddressOf SDR
'Start the process
P.Start()
'Begin async data reading
P.BeginOutputReadLine()
'"Map" our drive
P.StandardInput.WriteLine("pushd \\file-server\File-Server")
'Call our command, you could pass args here if you wanted
P.StandardInput.WriteLine("tree2.com c:\3ea7025b247d0dfb7731a50bf2632f")
'Once our command is done CMD.EXE will still be sitting around so manually exit
P.StandardInput.WriteLine("exit")
P.WaitForExit()
End Using
Me.Close()
End Sub
Private Sub SDR(ByVal sender As Object, ByVal e As DataReceivedEventArgs)
Trace.WriteLine(e.Data)
End Sub
其他提示
我跑过这个问题,接受的解决方案是复杂,我一点点。我所做的就是把UNC路径和它的内容复制到[Path.GetTempDir()]\[Guid.NewGuid().ToString()]
,然后用它作为我的工作目录process.StartInfo.WorkingDirectory
。在一个名为“环境”,这将实现IDisposable,并在处置清理您创建的临时目录类包装这个功能。像这样(忽略的设置的参考文献):
using (var env = new ProcessEnvironment(settings))
{
filePath = Path.Combine(env.WorkingDirectory, settings.ApplicationEXE);
var psi = new ProcessStartInfo
{
UseShellExecute = false,
FileName = filePath,
WorkingDirectory = env.WorkingDirectory,
Arguments = (args != null && args.Length > 0 ? String.Join(" ", args) : null)
};
var proc = Process.Start(psi);
if (env.ExecutingFromTempDir || settings.WaitForExit)
proc.WaitForExit();
}
和ProcessEnvironment看起来像:
class ProcessEnvironment : IDisposable
{
private Settings itsSettings;
private string itsTempDestDirectory;
public string WorkingDirectory { get; set; }
public bool ExecutingFromTempDir { get { return !String.IsNullOrEmpty(itsTempDestDirectory); } }
public ProcessEnvironment(Settings settings)
{
this.itsSettings = settings;
WorkingDirectory = GetWorkingDirectory();
}
private string GetWorkingDirectory()
{
var dirInfo = new DirectoryInfo(itsSettings.StartupFolder);
if (!IsUncDrive(dirInfo))
return itsSettings.StartupFolder;
return CreateWorkingDirectory(dirInfo);
}
private string CreateWorkingDirectory(DirectoryInfo dirInfo)
{
var srcPath = dirInfo.FullName;
itsTempDestDirectory = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(itsTempDestDirectory);
//Now Create all of the directories
foreach (string dirPath in Directory.GetDirectories(srcPath, "*", SearchOption.AllDirectories))
Directory.CreateDirectory(dirPath.Replace(srcPath, itsTempDestDirectory));
//Copy all the files & Replaces any files with the same name
foreach (string newPath in Directory.GetFiles(srcPath, "*.*", SearchOption.AllDirectories))
File.Copy(newPath, newPath.Replace(srcPath, itsTempDestDirectory), true);
return itsTempDestDirectory;
}
private bool IsUncDrive(DirectoryInfo dirInfo)
{
Uri uri = null;
if (!Uri.TryCreate(dirInfo.FullName, UriKind.Absolute, out uri))
{
return false;
}
return uri.IsUnc;
}
public void Dispose()
{
try
{
if (ExecutingFromTempDir)
Directory.Delete(itsTempDestDirectory, true);
}
catch (Exception ex)
{ //do nothing - if we can't delete then we can't do it
Console.WriteLine("Failed in Dispose: " + ex);
}
}
}
不隶属于 StackOverflow