我们可以从共享路径运行批处理文件(.bat)

时间:2022-02-02 05:37:55

I have a shared path (like //servername/c$/batches/) where all my batch files are located now I am writing an web application in C# to run .bat files from that application. I know way to do it when I have a physical path. But here I dont have a physical path. Is it possible to do it.

我有一个共享路径(比如// servername / c $ / batches /),我现在所有的批处理文件都在这里。我正在用C#编写一个Web应用程序来运行该应用程序的.bat文件。当我有物理路径时,我知道这样做的方法。但在这里,我没有物理路径。有可能做到这一点。

EDIT# 1

编辑#1

I execute my bat files just by double clicking on them or open the cmd progam on the physical server and then navigate to the drive and execute the bat file.

我只需双击它们就可以执行我的bat文件,或者在物理服务器上打开cmd程序,然后导航到驱动器并执行bat文件。

EDIT #2

编辑#2

when I put UNC path the get the following error

当我放入UNC路径时,得到以下错误

I getting an error myprogram.exe is not recognized as an internal or external command operable program or batch file. 9009

我收到错误myprogram.exe未被识别为内部或外部命令可操作程序或批处理文件。 9009

3 个解决方案

#1


14  

Batch files don't support UNC paths as their "current directory". There's a hackish work around of doing:

批处理文件不支持UNC路径作为其“当前目录”。这样做有一个疯狂的工作:

pushd "%~dp0"
your batch stuff
popd

%~dp0 expands to the current (d)rive/(p)ath/(0)batchfilename

%~dp0扩展为当前(d)rive /(p)ath /(0)批处理文件名


example:

例:

ok. a Simple batch file:

好。简单的批处理文件:

pushd %~dp0
echo "Hello from batch land"
echo %~dp0
popd

put that on a server somewhere, and try to run it via a unc path:

将它放在某个服务器上,并尝试通过unc路径运行它:

C:\> \\server\share\test.bat

You'll get as output:

你会得到输出:

C:\>pushd \\server\share\

Z:\>echo Hello from batch land
Hello from batch land

Z:\>echo \\server\share\
\\server\share\

Z:\>popd

C:\>

Weird, but it works.

很奇怪,但它确实有效。

#2


0  

That's called a UNC path.
You can use it just like any other path.

这被称为UNC路径。你可以像任何其他路径一样使用它。

However, the user that your ASP.Net code is running as must have read access to the network share.

但是,运行ASP.Net代码的用户必须具有对网络共享的读访问权限。

#3


0  

Apparently, you do have a current-directory issue.
The .bat file is trying to run myprogram.exe from the current directory.

显然,您确实有当前目录问题。 .bat文件正在尝试从当前目录运行myprogram.exe。

You can make a wrapper batch file on your local machine that maps the network share:

您可以在本地计算机上创建一个映射网络共享的包装器批处理文件:

pushd \\server\c$\dir
call filename.bat
popd

You can put this wrapper file anywhere, then call it from your code.

您可以将此包装文件放在任何位置,然后从您的代码中调用它。

#1


14  

Batch files don't support UNC paths as their "current directory". There's a hackish work around of doing:

批处理文件不支持UNC路径作为其“当前目录”。这样做有一个疯狂的工作:

pushd "%~dp0"
your batch stuff
popd

%~dp0 expands to the current (d)rive/(p)ath/(0)batchfilename

%~dp0扩展为当前(d)rive /(p)ath /(0)批处理文件名


example:

例:

ok. a Simple batch file:

好。简单的批处理文件:

pushd %~dp0
echo "Hello from batch land"
echo %~dp0
popd

put that on a server somewhere, and try to run it via a unc path:

将它放在某个服务器上,并尝试通过unc路径运行它:

C:\> \\server\share\test.bat

You'll get as output:

你会得到输出:

C:\>pushd \\server\share\

Z:\>echo Hello from batch land
Hello from batch land

Z:\>echo \\server\share\
\\server\share\

Z:\>popd

C:\>

Weird, but it works.

很奇怪,但它确实有效。

#2


0  

That's called a UNC path.
You can use it just like any other path.

这被称为UNC路径。你可以像任何其他路径一样使用它。

However, the user that your ASP.Net code is running as must have read access to the network share.

但是,运行ASP.Net代码的用户必须具有对网络共享的读访问权限。

#3


0  

Apparently, you do have a current-directory issue.
The .bat file is trying to run myprogram.exe from the current directory.

显然,您确实有当前目录问题。 .bat文件正在尝试从当前目录运行myprogram.exe。

You can make a wrapper batch file on your local machine that maps the network share:

您可以在本地计算机上创建一个映射网络共享的包装器批处理文件:

pushd \\server\c$\dir
call filename.bat
popd

You can put this wrapper file anywhere, then call it from your code.

您可以将此包装文件放在任何位置,然后从您的代码中调用它。