locked
Validate a Network folder path RRS feed

  • Question

  • Hello,

    I need to check from a textBox.Text if the information that was entered corresponds to a valid network folder path, it is to say, it should look like "\\Server01\folder1\foder2"

    Since when we write this kind of expresions in the explorer or outlook they get blue (it recognize it as a link) like the one that is above I would assume that there are already some functions in .net to check if this corresponds to a network location ...

    Question: 

    Is there any funciont aht would audit/validate a correct folder path (string)?

    Thanks, 


    Kikeman Electric Systems Engineer

    Saturday, February 23, 2013 6:24 PM

Answers

  • You have to use Regular Expression to validate whether text is valid network path. You can use following regular expression code

    using System.Text.RegularExpressions;
    
    string pattern = @"^\\{2}[\w-]+(\\{1}(([\w-][\w-\s]*[\w-]+[$$]?)|([\w-][$$]?$)))+";
    string[] names = { @"\\my-network\somelocation", @"\\my-network\\somelocation", 
        @"\\\my-network\somelocation", @"my-network\somelocation",
         @"\\my-network\\somelocation",@"\\my-network\somelocation\aa\dd",
         @"\\my-network\somelocation\",@"\\my-network\\somelocation"};
    foreach (string name in names)
    {
     if (Regex.IsMatch(name, pattern))
     {
      Console.WriteLine(name);
    //Directory.Exists function to check if file exists } }

    Else you can refer following link

    http://social.msdn.microsoft.com/Forums/en/csharplanguage/thread/0bfb59d0-c6ca-43c5-804a-f091e7cccd0e

    And you can check whether text is valid network path in TextChanged event, if valid you can use Directory.Exists function to know if folder exists or not. But as Christopher84 suggested, validating folder exists on network path could take sometime so better to have it on hyperlink click.


    Gaurav Khanna | Microsoft VB.NET MVP | Microsoft Community Contributor




    Gaurav Khanna | Microsoft VB.NET MVP | Microsoft Community Contributor

    • Proposed as answer by Lisa Zhu Monday, February 25, 2013 7:19 AM
    • Marked as answer by Lisa Zhu Tuesday, March 5, 2013 8:54 AM
    Monday, February 25, 2013 6:21 AM
  •  

    Question: 

    Is there any funciont aht would audit/validate a correct folder path (string)?

    Thanks, 


    Kikeman Electric Systems Engineer

    You can try Directory.Exists function 

    http://msdn.microsoft.com/en-in/library/system.io.directory.exists.aspx


    Gaurav Khanna | Microsoft VB.NET MVP | Microsoft Community Contributor

    • Proposed as answer by Lisa Zhu Monday, February 25, 2013 7:18 AM
    • Marked as answer by Lisa Zhu Tuesday, March 5, 2013 8:54 AM
    Saturday, February 23, 2013 6:55 PM

All replies

  •  

    Question: 

    Is there any funciont aht would audit/validate a correct folder path (string)?

    Thanks, 


    Kikeman Electric Systems Engineer

    You can try Directory.Exists function 

    http://msdn.microsoft.com/en-in/library/system.io.directory.exists.aspx


    Gaurav Khanna | Microsoft VB.NET MVP | Microsoft Community Contributor

    • Proposed as answer by Lisa Zhu Monday, February 25, 2013 7:18 AM
    • Marked as answer by Lisa Zhu Tuesday, March 5, 2013 8:54 AM
    Saturday, February 23, 2013 6:55 PM
  • This "mark as blue" and changing the cursor are beyond the abilities of a simply Textbox.

    For any form of localised formatting you need a RichText Box. And at least for Hyperlinks it's as simple as setting a boolean value to true and register an event:

    http://stackoverflow.com/questions/435607/c-sharp-how-can-i-make-a-hyperlink-work-in-a-richtextbox

    The Full process involves the MouseMove & MouseClick Events in collrelation with the Draw Method. So better use the code that exists and stay sane.


    Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2


    Saturday, February 23, 2013 7:47 PM
  • Thanks Christopher84

    What I want is just to verify that the text in the textbox correspont to a Network folder, it is to say, if some one writes just something like "today is a sunny day" then he will get an error after leaving the text box because that text does not represent a Directory path, something like \\Server01\SomeFoder1\SomeOtherFolder

    I was just wondering if explorer or most of the microsoft sofwares recognice this strings as links, what would be the C# function, class, method to know that those strings are links not any generic text ...


    Kikeman Electric Systems Engineer


    • Edited by Kikeman Saturday, February 23, 2013 8:13 PM
    Saturday, February 23, 2013 8:12 PM
  • I think you can use regular expression for that.. or you can check it for \\ or \..

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful". CRMHUNT http://crmhunt.com

    Sunday, February 24, 2013 4:10 AM
  • If you only want to know if this could be a Valid Network Path, you can use regular expression.

    If you want to know if that is the Network Path to a folder the user can see, the only way is to actually go and try to access it. The only way to verrify the existence and avalibility of a resource is trying to actually open it.

    Be advised that Network operations can take long times (try opening a Network Folder from Run-Dialog. Notice the time before you get the error message). So callind one Synchroniously on Focus loss is a very bad idea.

    You can make the reg-ex check each Focus loss (or even each TextChanged) Event. This is a extremely fast thing.

    But actually checking if the folder exists should only be done:

    Explicitly (on Click of a Button), Asynchroniously (using a background Worker or Tasks) or ideally both.


    Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2


    Sunday, February 24, 2013 4:25 AM
  • You have to use Regular Expression to validate whether text is valid network path. You can use following regular expression code

    using System.Text.RegularExpressions;
    
    string pattern = @"^\\{2}[\w-]+(\\{1}(([\w-][\w-\s]*[\w-]+[$$]?)|([\w-][$$]?$)))+";
    string[] names = { @"\\my-network\somelocation", @"\\my-network\\somelocation", 
        @"\\\my-network\somelocation", @"my-network\somelocation",
         @"\\my-network\\somelocation",@"\\my-network\somelocation\aa\dd",
         @"\\my-network\somelocation\",@"\\my-network\\somelocation"};
    foreach (string name in names)
    {
     if (Regex.IsMatch(name, pattern))
     {
      Console.WriteLine(name);
    //Directory.Exists function to check if file exists } }

    Else you can refer following link

    http://social.msdn.microsoft.com/Forums/en/csharplanguage/thread/0bfb59d0-c6ca-43c5-804a-f091e7cccd0e

    And you can check whether text is valid network path in TextChanged event, if valid you can use Directory.Exists function to know if folder exists or not. But as Christopher84 suggested, validating folder exists on network path could take sometime so better to have it on hyperlink click.


    Gaurav Khanna | Microsoft VB.NET MVP | Microsoft Community Contributor




    Gaurav Khanna | Microsoft VB.NET MVP | Microsoft Community Contributor

    • Proposed as answer by Lisa Zhu Monday, February 25, 2013 7:19 AM
    • Marked as answer by Lisa Zhu Tuesday, March 5, 2013 8:54 AM
    Monday, February 25, 2013 6:21 AM