简体   繁体   中英

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

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)

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:

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.

Also you can convert Virtual Path into a Physical Path using:

string rootPath = Server.MapPath("~");

Web Root Operator:

ASP.NET includes the Web application root operator (~), which you can use when specifying a path in server controls. ASP.NET resolves the ~ operator to the root of the current application. 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

尝试如下操作:

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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM