C #에서 절대 경로에 대한 상대 경로?
이미지에 대한 href 파일 경로 (예 : ".... \ images \ image.jpg")가 포함 된 xml 파일이 있습니다. href에는 상대 경로가 포함됩니다. 이제 hrefs를 이미지로 추출하고 파일 시스템의 절대 경로로 바꿔야합니다.
GetFullPath 메서드에 대해 알고 있지만 시도해 보았고 C :로 보이는 CurrentDirectory 집합에서만 작동하는 것 같아서 어떻게 사용할 수 있는지 모르겠습니다. 그리고 여전히 나는 hrefs를 포함하는 파일의 절대 경로와 href 상대 경로를 가지고 있으므로 절대 경로를 기준으로 ".... \"부분의 수를 다시 세는 것이 간단한 작업이기 때문에 포함하는 파일을 프로그래밍 방식으로 수행 할 수있는 방법이 있어야합니다.
내가 모르는 간단한 방법이 있기를 바랍니다! 어떤 아이디어?
XML 파일이 Path.Combine을 사용하는 실제 디렉토리를 알고 있다고 가정합니다.
var absolute_path = Path.Combine(directoryXmlLivesIn, "..\images\image.jpg");
..가 축소 된 상태에서 전체 경로를 되돌리려면 다음을 사용할 수 있습니다.
Path.GetFullPath((new Uri(absolute_path)).LocalPath);
string exactPath = Path.GetFullPath(yourRelativePath);
공장
이것은 효과가 있었다.
var s = Path.Combine(@"C:\some\location", @"..\other\file.txt");
s = Path.GetFullPath(s);
Server.MapPath
방법 을 시도해 보셨습니까? 다음은 예입니다.
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
상대 경로를 절대 경로로 변환하는 가장 좋은 방법입니다!
string absolutePath = System.IO.Path.GetFullPath(relativePath);
Path.Combine을 "기본"경로와 함께 사용한 다음 결과에 GetFullPath를 사용할 수 있습니다.
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
http://msdn.microsoft.com/en-us/library/fyy7a5kt.aspx를 살펴보십시오 Path.Combine
.
이것은 나를 위해 일했습니다.
//used in an ASP.NET MVC app
private const string BatchFilePath = "/MyBatchFileDirectory/Mybatchfiles.bat";
var batchFile = HttpContext.Current.Server.MapPath(BatchFilePath);
참고 URL : https://stackoverflow.com/questions/4796254/relative-path-to-absolute-path-in-c
'program story' 카테고리의 다른 글
C #에서 인라인 함수를 만드는 방법 (0) | 2020.10.20 |
---|---|
간단한 PHP 개발 서버가 있습니까? (0) | 2020.10.20 |
node.js에서 로그인 인증을 구현하는 방법 (0) | 2020.10.20 |
EmberJS : 동일한 경로에서 여러 모델을로드하는 방법은 무엇입니까? (0) | 2020.10.20 |
ES6를 사용한 VS 코드 (0) | 2020.10.20 |