开发者

Can c# understand this kind of pathing for pointing a file?

开发者 https://www.devze.com 2023-01-24 15:13 出处:网络
..\\..\\..\\ConnectionInterface\\ConnectionInterface.vbproj I mean the \"..\\\" Because I am reading up a .sln file as a text file to get all the projects in 开发者_如何学运维that solution and the p

..\..\..\ConnectionInterface\ConnectionInterface.vbproj

I mean the "..\"

Because I am reading up a .sln file as a text file to get all the projects in 开发者_如何学运维that solution and the problem is this projects inside where in different directories or level.

Here is an example

..\..\..\ConnectionInterface\ConnectionInterface.vbproj ..\States\Components\States.vbproj

any ideas how to get the actual paths of these projects?


Path.GetFullPath(@"..\..\..\ConnectionInterface\ConnectionInterface.vbproj");

This is relative to the current working directory, therefore if the relative reference is not based on the current working directory you will need to define that first.


You can use Path.Combine, but you'll need to know where it's relative to. Basically find the directory that contains the original .sln file (e.g. using Path.GetDirectoryName and Path.GetFullPath) and then use Path.Combine to combine the original directory with the relative file.

For example:

using System;
using System.IO;
using System.Linq;
using System.Xml.Linq;

class Test
{        
    static void Main()
    {
        string originalFile = "Test.cs";
        string relative = @"..\Documents\Foo";

        string originalAbsoluteFile = Path.GetFullPath(originalFile);        
        string originalDirectory = Path.GetDirectoryName(originalAbsoluteFile);
        string combined = Path.Combine(originalDirectory, relative);
        string combinedAbsolute = Path.GetFullPath(combined);
        Console.WriteLine(combinedAbsolute);
    }
}


The question isn't very clear, but if you mean does C# understand: C:\SomeDir\InnerDir1\ ..\InnerDir2 to resolve to C:\SomeDir\InnerDir2, then yes, it will work. Just append directory the solution file is in with the relative path, and you are done.

0

精彩评论

暂无评论...
验证码 换一张
取 消