如何在 DOS 中指定相对本地文件路径?
可能的重复:
如何在 DOS 中动态指定文件?
我正在使用 c#.NET 2.0 执行 DOS 命令来 ftp 文件。除了一件事之外,所有工作都有效,在我调用的 cmd 文件中,它运行一个 PUT 语句。现在 put 语句有一个硬编码的本地文件路径。我需要指定一个动态路径。我已经尝试过
put %~dp0\myfile.DTL myfile.dtl
,但它说找不到该文件。
现在,.NET 代码调用一个 BAT 文件,该文件仅用于调用 CMD 文件。有趣的是,BAT 文件在对 CMD 文件的调用中确实成功地使用了相对路径:
ftp.exe -s:%~dp0\oit.cmd
但是,我无法在 cmd 文件中获取该相对路径:
open <my host>
<user name>
<password>
put <hardcoded path that needs to be relative path>localfilename remotefilename
我永远不知道它存在的位置,所以我只需要获取文件所在的任何本地目录。
Possible Duplicate:
How do I dynamically specify a file in DOS?
I am using c#.NET 2.0 to execute DOS commands to ftp a file. All works except for 1 thing, in the cmd file I call, it runs a PUT statement. Right now the put statement has a hardcoded local file path. I need to specify a dynamic path. I've tried
put %~dp0\myfile.DTL myfile.dtl
but it says it can't find the file.
Right now the .NET code calls a BAT file which only exist to call the CMD file. Interestingly, the BAT file DOES successfully use a relative path in its call to the CMD file:
ftp.exe -s:%~dp0\oit.cmd
However, I can't get that relative path to wrok in the cmd file:
open <my host>
<user name>
<password>
put <hardcoded path that needs to be relative path>localfilename remotefilename
I'll bever know where it will exist so I just need to grab whatever local directorey the file is in.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
亲戚是“。” (点)。
能发一下具体情况吗?你在哪个目录,文件在哪里?
小心“~”字符...它对于 DOS 文件有特殊含义(8.3 表示法)
Relative is "." (dot).
Can you post the exact situation? What directory are you in, and where is the file?
Be careful with the "~" char ... it has a special meaning for DOS files (8.3 notation)
使用 System.IO.Directory.GetCurrentDirectory() 获取当前(工作)文件夹。
或者,如果您想要相对于 .EXE 文件的路径,可以使用 System.Reflection.Assembly.GetEntryAssembly().CodeBase 获取 .EXE 文件的完整路径,然后使用
System.IO.Path.GetDirectoryName()
获取该路径的目录名称。之后,您可以使用 System.IO.Path.Combine 来从相对路径中获取绝对路径:
Use
System.IO.Directory.GetCurrentDirectory()
to get the current (working) folder.Alternatively, if you want the path relative to your .EXE file, you can use
System.Reflection.Assembly.GetEntryAssembly().CodeBase
to get full path to your .EXE file, then useSystem.IO.Path.GetDirectoryName()
to get the directory name of that path.After that, you can use
System.IO.Path.Combine
to get absolute path out of relative: