


Note that we have never touched the file system. Then, we can get the parent directory, and we can repeat this operation any number of times to navigate further up: // This is reliable if path results from this or the previous operation GetDirectoryName will return null for an empty path, which we coalesce to "".įirst, normalize the input path: dirPath = Path.GetDirectoryName(dirPath + "/") ? "" // To handle nulls, we append "/" rather than '/'.This includes the removal of any trailing slashes.GetDirectoryName will normalize slashes and navigational dots.GetDirectoryName will refrain from discarding the last path component thanks to the added slash.Adding a slash (as string, not as char) will treat a null path the same as it treats "".Path.GetDirectoryName(path + "/") ? "" will reliably give us a directory path without a trailing slash. Actually, we will provide the child items ('C:\OIMShare1\abc1\') in the script and it has identify the parent folder ('C:\OIMShare1\') and the child items need to inherit all the permissions for the parent. What can we do? We will build on the following discovery: Is there a way to identify the parent folder if we provide the child items in the script. Without going through the file system, navigating in that manner does not provide us with a normalized, properly comparable path. My other answer on this page works for operations that put the file system to work, but if we want to have the resolved path right now (such as for comparison reasons), without going through the file system, C:/Temp/. Note that some answers to similar questions try to use the Uri type, but that struggles with trailing slashes vs. Since nothing else I have found helps to solve this in a truly normalized way, here is another answer. follows the same rules, twice in a => D:\Grandparent\ => D:\ will chop that ".") => D:\Grandparent\Parent\ to a path in cmd or explorer and it will behave exactly as I describe below. behaves exactly like it does everywhere else in Windows. Path.Combine makes sure you never go wrong with slashes. Even more so if what northben says is true, that GetParent requires an extra call if there is a trailing slash. I've found variants of System.IO.Path.Combine(myPath, ".") to be the easiest and most reliable.
#Installbuilder get parent folder mac#
In a DOS, Windows or Mac command line, two dots (.) refer to the preceding folder/directory. By contrast, this answer achieves the result and normalization purely using path strings, without using the file system at all. A folder that is one level up from the current directory in a file hierarchy. Edit: The normalization covered in this answer only happens when the path is used to access the file system, but not on the string itself.
