[英]how do i use the relative path to the scripts folder in my asp .net / c# project code?
this question should be relatively simple and shouldnt even require much explanation i dont think, here goes: 这个问题应该是相对简单的,我不认为甚至不需要太多解释,这里是:
my website is executing a batch file using the Process object which requires a path to the .bat file of course 我的网站正在使用Process对象执行批处理文件,该对象当然需要指向.bat文件的路径
i will be housing my site on multiple servers therefore i dont want to give an absolute path i want to just use a relative path within my project (for example use the scripts folder automatically generated by visual studio when i made my project) 我将站点放置在多台服务器上,因此我不想提供绝对路径,而只想在项目中使用相对路径(例如,使用我在创建项目时由Visual Studio自动生成的脚本文件夹)
i just am not sure exactly how to reference that file within the code once its in the scripts folder. 我只是不确定究竟如何在脚本文件夹中的代码中引用该文件。
here is my current code for referencing my .bat: 这是我当前引用我的.bat的代码:
p.StartInfo.WorkingDirectory = "C:\\Users\\e\\Desktop\\Test_Bat_Thing";
p.StartInfo.FileName = "C:\\Users\\e\\Desktop\\Test_Bat_Thing\\test.bat";
which works perfectly. 完美地运作。 but i want it to be something along the lines of 但我希望它能够像
p.StartInfo.WorkingDirectory = ".\\scripts";
p.StartInfo.FileName = ".\\scripts\\test.bat";
please help! 请帮忙! thanks in advance :) 提前致谢 :)
Try out using "~/"
(Web Root Operator) as reference to the site web root folder. 尝试使用"~/"
(Web根操作员)作为对站点Web根文件夹的引用。
Also you can convert Virtual Path into a Physical Path using: 您还可以使用以下方法将虚拟路径转换为物理路径:
string rootPath = Server.MapPath("~");
Web Root Operator: Web根运算符:
ASP.NET includes the Web application root operator (~), which you can use when specifying a path in server controls. ASP.NET包含Web应用程序根运算符(〜),您可以在服务器控件中指定路径时使用它。 ASP.NET resolves the ~ operator to the root of the current application. ASP.NET将〜运算符解析为当前应用程序的根。 You can use the ~ operator in conjunction with folders to specify a path that is based on the current root. 您可以将〜运算符与文件夹结合使用,以指定基于当前根目录的路径。
For more details see on MSDN: ASP.NET Web Project Paths 有关更多详细信息,请参见MSDN: ASP.NET Web项目路径
尝试如下操作:
p.StartInfo.FileName = this.Server.MapPath("test.bat")
If you need to reference the Desktop Path of the current user, you can use this statement 如果需要引用当前用户的桌面路径,则可以使用此语句
string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
your code will look like 您的代码看起来像
p.StartInfo.FileName = System.IO.Path.Combine(desktopPath, "\\Test_Bat_Thing\\test.bat");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.