Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Opening any file

Opening any file

Scheduled Pinned Locked Moved C#
xmltutorialquestion
8 Posts 4 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Dylan van Heerden
    wrote on last edited by
    #1

    How do you open a file for example an XML file in coding? I tried "File.Open(Path including filename,FileMode.Open,FileAccess.Read)" but it does not seem to work. Am I missing something? Sweet

    T S 2 Replies Last reply
    0
    • D Dylan van Heerden

      How do you open a file for example an XML file in coding? I tried "File.Open(Path including filename,FileMode.Open,FileAccess.Read)" but it does not seem to work. Am I missing something? Sweet

      T Offline
      T Offline
      the last free name
      wrote on last edited by
      #2

      I usually go for something like this:- XmlTextReader reader = new XmlTextReader(file) and then read it with:- while (!reader.EOF) { if(reader.NodeType == XmlNodeType.Element) { switch (reader.Name) { case "AnInterestingNode": // do stuff break; } } reader.Read() // advances the reader } The only thing to look out for is if you use reader.ReadOuterXML(); in a case statemant as this advances the reader, and the 'reader.Read();' call means you will miss a node. (This very thing has me scratching my head for about an hour!)

      D 1 Reply Last reply
      0
      • T the last free name

        I usually go for something like this:- XmlTextReader reader = new XmlTextReader(file) and then read it with:- while (!reader.EOF) { if(reader.NodeType == XmlNodeType.Element) { switch (reader.Name) { case "AnInterestingNode": // do stuff break; } } reader.Read() // advances the reader } The only thing to look out for is if you use reader.ReadOuterXML(); in a case statemant as this advances the reader, and the 'reader.Read();' call means you will miss a node. (This very thing has me scratching my head for about an hour!)

        D Offline
        D Offline
        Dylan van Heerden
        wrote on last edited by
        #3

        Ok what I want to do is open the file like you would normally double click on it in a directory. Is it possible? Sweet

        T 1 Reply Last reply
        0
        • D Dylan van Heerden

          Ok what I want to do is open the file like you would normally double click on it in a directory. Is it possible? Sweet

          T Offline
          T Offline
          the last free name
          wrote on last edited by
          #4

          you mean open a file in a supporting application eg Notepad and a .txt file? if so Process.Start("Notepad.exe", "C:\\temp.txt"); If environment variable 'path' doesn't complete the app path you will have to give it in full.

          1 Reply Last reply
          0
          • D Dylan van Heerden

            How do you open a file for example an XML file in coding? I tried "File.Open(Path including filename,FileMode.Open,FileAccess.Read)" but it does not seem to work. Am I missing something? Sweet

            S Offline
            S Offline
            sreejith ss nair
            wrote on last edited by
            #5

            So i think you know how to get a required file and file name. //pass your file name as parameter //use this code below to open your file like normal double clicking. System.Diagnostics.Process.Start("filename") :-O Sreejith S S Nair

            D 1 Reply Last reply
            0
            • S sreejith ss nair

              So i think you know how to get a required file and file name. //pass your file name as parameter //use this code below to open your file like normal double clicking. System.Diagnostics.Process.Start("filename") :-O Sreejith S S Nair

              D Offline
              D Offline
              Dylan van Heerden
              wrote on last edited by
              #6

              I tried the following try { DirectoryInfo dirInfo = Directory.GetParent("C:\\Club"); FileInfo[] Files = dirInfo.GetFiles("*.xml"); MessageBox.Show(Files[0].ToString()); Process.Start(Files[0].ToString()); } catch(Exception Error) { MessageBox.Show(Error.Message); } and I recieve the following error "The system cannot find the file specified" :doh: Sweet

              D 1 Reply Last reply
              0
              • D Dylan van Heerden

                I tried the following try { DirectoryInfo dirInfo = Directory.GetParent("C:\\Club"); FileInfo[] Files = dirInfo.GetFiles("*.xml"); MessageBox.Show(Files[0].ToString()); Process.Start(Files[0].ToString()); } catch(Exception Error) { MessageBox.Show(Error.Message); } and I recieve the following error "The system cannot find the file specified" :doh: Sweet

                D Offline
                D Offline
                Dave Kreskowiak
                wrote on last edited by
                #7

                _Hacker wrote: MessageBox.Show(Files[0].ToString()); Process.Start(Files[0].ToString()); I bet the MessageBox didn't show the FULL path to the file you tried to open, did it? The following code changes will work because the code will no longer assume a default directory with using just Files[]:

                try
                {
                String parentPath = Directory.GetParent("C:\\Club");
                String fullFilePath;
                DirectoryInfo dirInfo = New DirectoryInfo(parentPath);
                 
                FileInfo[] Files = dirInfo.GetFiles("*.xml");
                fullFilePath = Path.Combine(parentPath, Files[0]);
                MessageBox.Show(fullFilePath);
                Process.Start(fullFilePath);
                }
                catch(Exception Error)
                {
                MessageBox.Show(Error.Message);
                }

                RageInTheMachine9532 "...a pungent, gastly, stinky piece of cheese!" -- The Roaming Gnome

                D 1 Reply Last reply
                0
                • D Dave Kreskowiak

                  _Hacker wrote: MessageBox.Show(Files[0].ToString()); Process.Start(Files[0].ToString()); I bet the MessageBox didn't show the FULL path to the file you tried to open, did it? The following code changes will work because the code will no longer assume a default directory with using just Files[]:

                  try
                  {
                  String parentPath = Directory.GetParent("C:\\Club");
                  String fullFilePath;
                  DirectoryInfo dirInfo = New DirectoryInfo(parentPath);
                   
                  FileInfo[] Files = dirInfo.GetFiles("*.xml");
                  fullFilePath = Path.Combine(parentPath, Files[0]);
                  MessageBox.Show(fullFilePath);
                  Process.Start(fullFilePath);
                  }
                  catch(Exception Error)
                  {
                  MessageBox.Show(Error.Message);
                  }

                  RageInTheMachine9532 "...a pungent, gastly, stinky piece of cheese!" -- The Roaming Gnome

                  D Offline
                  D Offline
                  Dylan van Heerden
                  wrote on last edited by
                  #8

                  Thank you all... It works! Sweet

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups