简体   繁体   English

如何在asp .net / c#项目代码中使用脚本文件夹的相对路径?

[英]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.

相关问题 C#ASP.NET在我的项目中查找文件夹的路径 - C# ASP.NET Finding the path of a folder WITHIN my project 如何在C#/ .NET中将相对路径转换为完全限定的路径? - How do I convert a relative path to a fully qualified path in C# / .NET? 如何在ASP.NET MVC C#项目中插入脚本 - How to insert scripts in an asp.net mvc c# project 将存储在我的项目文件夹中的图像的相对路径添加到SQL Server表中,然后将其恢复到程序C#中 - Add into SQL Server table a relative path of an image stored in my project folder, then recover it into my program C# C#代码:这是ASP.NET AJAX脚本的工作方式吗? - C# Code: Is this how ASP.NET AJAX scripts work? (Visual Studio) 如何将 powershell 脚本导入 c# 项目,以便我可以将 .NET 应用程序与脚本一起发布 - (Visual Studio) How to import powershell scripts into c# project so that I can publish my .NET application together with the scripts ASP.NET 核心 - 如何从我的 C# class 到我的 Z558B5744CF685F39D34EZ906B 客户端应用程序中获取我的状态代码? - ASP.NET Core - How do I get my status code from my C# class to my TypeScript Client App? c#如何打开一个Word文件并设置相对于项目文件夹的路径 - c# How to open a Word file and set the path is relative to the project folder 我可以在我的 C# ASP.NET MVC 应用程序中将 App_Data 文件夹用作普通文件夹吗? - Can I use App_Data folder as a normal folder in my C# ASP.NET MVC application? 如何在另一个脚本if语句中使用移动输入结果? C#Unity - How do I use my mobile input results in another scripts if statement? C# Unity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM