CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jun 2008
    Posts
    154

    [RESOLVED] Check Directory for xml file

    How do I simply check a directory to make sure there is a xml file there?? I don't need to load it > psuedo code

    Code:
    try
    {
        string location = @"\My Documents\My Games\Something\Something";
        check for (".xml");
    }
    catch
    {
        exception
    }

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Check Directory for xml file

    System.IO.File.Exists(string filename)

    Your Google-fu is weak young padawan....

  3. #3
    Join Date
    Jun 2008
    Posts
    154

    Re: Check Directory for xml file

    Awesome thanks!

  4. #4
    Join Date
    Jun 2008
    Posts
    154

    Re: Check Directory for xml file

    I got a problem

    I am using this as the argument

    Code:
                if (File.Exists(GameRef.GetDocumentsDirectory + @"\My Documents\My Games\RPGBXL" + "*.xml")) 
                {
                    manager.ChangeScreens(GameRef.PopUpScreenLoadFile);
                }
                else
                {
                    BeginException = true;
                }
    only problem is - IF there is a xml document there - Which there is, it still going to the exception

  5. #5
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Check Directory for xml file

    Looks like you are missing a backslash at the end of your path
    Unless you are looking for RPGBXL*.xml
    Always use [code][/code] tags when posting code.

  6. #6
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Check Directory for xml file

    Pretty sure you can't pass a wild card to that method either. In the case of looking for any .xml file, use Directory.GetFiles.

  7. #7

    Re: Check Directory for xml file

    Hi Bixel,
    Hope below code helps yo.. even yo need to search for txt files change the *xml to *txt

    DirectoryInfo dir = new DirectoryInfo(@"C:\Data");
    FileSystemInfo[] files = dir.GetFileSystemInfos("*xml");
    foreach (FileSystemInfo info in files)
    {
    Console.WriteLine(info.FullName);
    }

  8. #8
    Join Date
    Jun 2008
    Posts
    154

    Re: Check Directory for xml file

    to : ramkumarradhakrishnan

    That helped A LOT!!!

    here is successful code

    Code:
                DirectoryInfo dir = new DirectoryInfo(GameRef.GetDocumentsDirectory + @"\My Documents\My Games\RPGBXL");
                FileSystemInfo[] files = dir.GetFileSystemInfos("*xml");
                if(files[0] != null)
                {
                    manager.ChangeScreens(GameRef.PopUpScreenLoadFile);
                }
                else
                {
    
                    BeginException = true;
    
                }
    Thanks!
    Last edited by bixel; August 18th, 2010 at 07:21 PM. Reason: solved

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured