Relative path to absolute path in C#?

前端 未结 8 2044
难免孤独
难免孤独 2020-12-02 14:14

I have xml files that contain href file paths to images (e.g. \"....\\images\\image.jpg\"). The hrefs contain relative paths. Now, I need to extract the hrefs to the images

相关标签:
8条回答
  • 2020-12-02 14:27

    It`s best way for convert the Relative path to the absolute path!

    string absolutePath = System.IO.Path.GetFullPath(relativePath);
    
    0 讨论(0)
  • 2020-12-02 14:28
    string exactPath = Path.GetFullPath(yourRelativePath);
    

    works

    0 讨论(0)
  • 2020-12-02 14:31

    Assuming you know the real directory the XML file lives in use Path.Combine, e.g.

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

    If you want to get back the full path with any ..'s collapsed then you can use:

    Path.GetFullPath((new Uri(absolute_path)).LocalPath);
    
    0 讨论(0)
  • 2020-12-02 14:32

    This worked for me.

    //used in an ASP.NET MVC app
    private const string BatchFilePath = "/MyBatchFileDirectory/Mybatchfiles.bat"; 
    var batchFile = HttpContext.Current.Server.MapPath(BatchFilePath);
    
    0 讨论(0)
  • 2020-12-02 14:35

    Have you tried Server.MapPath method. Here is an example

    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
    
    0 讨论(0)
  • 2020-12-02 14:45

    This worked.

    var s = Path.Combine(@"C:\some\location", @"..\other\file.txt");
    s = Path.GetFullPath(s);
    
    0 讨论(0)
提交回复
热议问题