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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7503944/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 18:07:36  来源:igfitidea点击:

Can we run a batch file (.bat) from the shared path

c#.netasp.netwindowsbatch-file

提问by Praneeth

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 filesfrom 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.

我只是通过双击它们或打开物理服务器上的 cmd 程序来执行我的 bat 文件,然后导航到驱动器并执行 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

回答by Marc B

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)batchfilename



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.

奇怪,但它有效。

回答by SLaks

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 代码的用户必须对网络共享具有读取访问权限。

回答by SLaks

Apparently, you dohave a current-directory issue.
The .batfile is trying to run myprogram.exefrom 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.

你可以把这个包装文件放在任何地方,然后从你的代码中调用它。