0

I need to identify first is this a UNCPath if so get the file directory Is there a method to identify if a path is a UNC Path? How do I get the file's parent.Parent.directory?

\\MyServer\\MySharedDrive\\MyDirectory\\MySubDirectory\\Myfile.csv

 Wanted result and should work however deep

 \\MyServer\\MySharedDrive\\MyDirectory

so that I can save another file to the above directory. I guess I cannot do

  Path.Combine("\\MyServer\\MySharedDrive\\MyDirectory",myNewFile.csv)

Any Suggestions?

Many thanks

user9969
  • 15,632
  • 39
  • 107
  • 175

2 Answers2

0

From MSDN

Most members of the Path class do not interact with the file system and do not verify the existence of the file specified by a path string

Again from MSDN on Path class

In members that accept a path, the path can refer to a file or just a directory. The specified path can also refer to a relative path or a Universal Naming Convention (UNC) path for a server and share name

Said that, you could write

string myUNCPath = @"\\MyServer\MySharedDrive\MyDirectory\MySubDirectory\Myfile.csv";
string myParent = Path.GetDirectoryName(Path.GetDirectoryName(myUNCPath));
string finalFile = Path.Combine(@"\\MyServer\MySharedDrive\MyDirectory","myNewFile.csv");

As a safety check you should execute two separate calls to Path.GetDirectoryName because if you have only one level deep of subdirectory then the result of Path.GetDirectoryName will be null

For example, if the initial UNC path is

string myUNCPath = @"\\MyServer\MySharedDrive\MyDirectory\Myfile.csv";
string myParent = Path.GetDirectoryName(myUNCPath);
if(!string.IsNullOrWhiteSpace(myParent))
{
     myParent = Path.GetDirectoryName(myParent);
     if(!string.IsNullOrWhiteSpace(myParent))
     {
         string finalFile = Path.Combine(myParent, "myNewFile.csv");
         .....
     }
 }

For the part relative to your first question, the discovery of an UNC path is relatively easy. See this article on Windows Dev Center about Paths and Files

Console.WriteLine(IsUNCPath(myUNCPath));
......

bool IsUNCPath(string pathToCheck)
{
    return pathToCheck.StartsWith(@"\\");
}
Steve
  • 213,761
  • 22
  • 232
  • 286
0

To get parent directory and then creating a new path you can do:

string path = "\\MyServer\\MySharedDrive\\MyDirectory\\MySubDirectory\\Myfile.csv";
DirectoryInfo directory = new DirectoryInfo(Path.GetDirectoryName(path));
string finalPath = Path.Combine(directory.Parent.FullName, "myNewFile.csv"

To check if the path is UNC check this post.

Community
  • 1
  • 1
Habib
  • 219,104
  • 29
  • 407
  • 436
  • Hi thanks for your time that works!! is this enough to check if its a unc path myPath.Contains(@"\\")) – user9969 Jul 04 '14 at 14:02
  • no , not at all, **at least** the check should be to check if the path starts with double back slash like `myPath.StartsWith("\\\\")`, But you should see this post for detailed answer. http://stackoverflow.com/questions/520753/what-is-the-correct-way-to-check-if-a-path-is-an-unc-path-or-a-local-path – Habib Jul 04 '14 at 14:05