Visual C# Developer Center > Visual C# Forums > Visual C# Language > Check if path is a networkadress or a path to a local file
Ask a questionAsk a question
 

AnswerCheck if path is a networkadress or a path to a local file

  • Wednesday, November 04, 2009 9:46 PMmartin.doku Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hello,

    how can i check if my path is a networkadress or a localpath? I am working with FileInfo and DirectoryInfo.
    I would differentiat it parsing the path. I check if the path is starting with a character or with \\.

    Isnt there a better solution?

    http://get.lima-city.de

Answers

  • Wednesday, November 04, 2009 10:04 PMBalaji Baskar Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    One way of checking is by getting root path:
    string fileName1 = System.IO.Path.GetPathRoot(@"C:\Program Files\TextPad 4\TextPad.exe");
    MessageBox.Show(fileName1);
    //Prints "C:\"
    string fileName2 = System.IO.Path.GetPathRoot(@"\\fs01\apps\Adobe\rs405eng.exe");
    MessageBox.Show(fileName2);
    //Prints "\\fs01\apps"
    
    
    

    the other way is the more simple, check whether the first two characters are "\\"

    fileName2.StartsWith("\\").ToString()
    //Prints "True"
    
    

    Balaji Baskar [Please mark the post as answer if it answers your question]
    • Marked As Answer bymartin.doku Thursday, November 05, 2009 9:33 AM
    •  
  • Thursday, November 05, 2009 9:34 AMmartin.doku Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hello,

    isnt there a nicer solution. I tried DriveInfo out, but if the path is to a networkresource DriveInfo will throug a Argumentexception. But for the first, i will take the path parse solution. :)

    I will use this regex pattern to check if the path is a local path.

    ^[A-Z]+[a-zA-Z]*:\\



    http://get.lima-city.de
    • Marked As Answer bymartin.doku Thursday, November 05, 2009 10:23 AM
    •  

All Replies

  • Wednesday, November 04, 2009 10:04 PMBalaji Baskar Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    One way of checking is by getting root path:
    string fileName1 = System.IO.Path.GetPathRoot(@"C:\Program Files\TextPad 4\TextPad.exe");
    MessageBox.Show(fileName1);
    //Prints "C:\"
    string fileName2 = System.IO.Path.GetPathRoot(@"\\fs01\apps\Adobe\rs405eng.exe");
    MessageBox.Show(fileName2);
    //Prints "\\fs01\apps"
    
    
    

    the other way is the more simple, check whether the first two characters are "\\"

    fileName2.StartsWith("\\").ToString()
    //Prints "True"
    
    

    Balaji Baskar [Please mark the post as answer if it answers your question]
    • Marked As Answer bymartin.doku Thursday, November 05, 2009 9:33 AM
    •  
  • Thursday, November 05, 2009 9:34 AMmartin.doku Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hello,

    isnt there a nicer solution. I tried DriveInfo out, but if the path is to a networkresource DriveInfo will throug a Argumentexception. But for the first, i will take the path parse solution. :)

    I will use this regex pattern to check if the path is a local path.

    ^[A-Z]+[a-zA-Z]*:\\



    http://get.lima-city.de
    • Marked As Answer bymartin.doku Thursday, November 05, 2009 10:23 AM
    •