The file / folder restriction is written in paths other than the specified root

In .NET, is there any API that allows me to limit some I / O operations to the specified path?

For example:

  • Root Path: C:\Test\

Now I want to call a function like this:

  • IO.Directory.CreateDirectory("../testing/",Root)

And now I want the API to handle this situation for me and not create a folder other than the specified directory.

So this should be created as c:\Test\testing\instead c:\testing\.

I know that I can do it manually, but I thought that maybe there is an API that I don’t know that supports such a thing.

The fact is that I have a bunch of random lines, and I will split folders and files based on them, I do not want to write anything to another folder, because one of these lines includes a line like "../"

+3
4

* nix envoirnements, chroot (jailroot). , - .net, goro chroot .net( jailroot .net).

+1

:

DirectoryInfo.CreateSubdirectory()

, , , .

0

DirectoryInfo, Path.Combine.

// Extension Method
public static class DirectoryExtension
{
    public static void CreateDirectory(this DirectoryInfo root, string path)
    {
        if (!Directory.Exists(root.FullName))
        {
            Directory.CreateDirectory(root.FullName);
        }

        string combinedPath = Path.Combine(root.FullName, path);
        Directory.CreateDirectory(combinedPath);
    }
}

// Usage
DirectoryInfo root = new DirectoryInfo(@"c:\Test");
root.CreateDirectory("testing");
root.CreateDirectory("testing123");
root.CreateDirectory("testingabc");
root.CreateDirectory(@"abc\123");
0

- .NET API, , . , Path.Combine(), , , , .

0

Source: https://habr.com/ru/post/1721614/


All Articles