Ścieżka względna do ścieżki absolutnej w C#?

Mam pliki xml, które zawierają ścieżki plików href do obrazów (np."....\ images \ image.jpg"). Href zawierają ścieżki względne. Teraz muszę wyodrębnić hrefs do obrazów i zamienić je w ścieżki absolutne w systemie plików.

Wiem o metodzie GetFullPath, ale wypróbowałem ją i wydaje się, że działa tylko z zestawu CurrentDirectory, który wygląda na C:, więc nie widzę, jak mógłbym tego użyć. I nadal mam ścieżkę bezwzględną pliku zawierającego hrefs i href ścieżki względne, więc ponieważ jest to dla mnie proste zadanie, aby liczyć z powrotem liczbę "....\ "części bazując na bezwzględnej ścieżce pliku zawierającego, wydaje się, że musi być sposób, aby to zrobić również programowo.

Mam nadzieję, że jest jakaś prosta metoda, o której Nie wiem! Jakieś pomysły?

Author: Anders, 2011-01-25

7 answers

Zakładając, że znasz prawdziwy katalog, plik XML żyje w use Path.Połącz, np.

var absolute_path = Path.Combine(directoryXmlLivesIn, "..\images\image.jpg");

Jeśli chcesz wrócić pełną ścieżkę z dowolnym ..'s collapsed then you can use:

Path.GetFullPath((new Uri(absolute_path)).LocalPath);
 81
Author: Paolo,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-01-25 16:46:47
string exactPath = Path.GetFullPath(yourRelativePath);

Działa

 90
Author: cahit beyaz,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-01-16 23:42:09

To zadziałało.

var s = Path.Combine(@"C:\some\location", @"..\other\file.txt");
s = Path.GetFullPath(s);
 28
Author: David Crowell,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-01-25 16:48:31

Czy próbowałeś Server.MapPath metoda. Oto przykład

string relative_path = "/Content/img/Upload/Reports/59/44A0446_59-1.jpg";
string absolute_path = Server.MapPath(relative_path);
//will be c:\users\.....\Content\img\Upload\Reports\59\44A0446_59-1.jpg
 5
Author: Tianzhen Lin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-05-17 16:36:49

Możesz użyć ścieżki.Połącz ze ścieżką" base", a następnie GetFullPath na wynikach.

string absPathContainingHrefs = GetAbsolutePath(); // Get the "base" path
string fullPath = Path.Combine(absPathContainingHrefs, @"..\..\images\image.jpg");
fullPath = Path.GetFullPath(fullPath);  // Will turn the above into a proper abs path
 3
Author: Reed Copsey,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-01-25 16:43:28
 0
Author: JeremyWeir,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-01-25 16:43:32

To mi pomogło.

//used in an ASP.NET MVC app
private const string BatchFilePath = "/MyBatchFileDirectory/Mybatchfiles.bat"; 
var batchFile = HttpContext.Current.Server.MapPath(BatchFilePath);
 0
Author: Jonny Boy,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-04-11 13:15:21