File not found exception

This is a discussion on File not found exception within the C# Programming forums, part of the General Programming Boards category; Hey there. Googled a bit to see if it was a common mistake / error but I found nothing. Basically ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Location
    Montreal, Quebec, Canada
    Posts
    73

    File not found exception

    Hey there. Googled a bit to see if it was a common mistake / error but I found nothing.

    Basically I just want to open a file and check its contents. Everything compiles without error but when the program runs it outputs nothing and throws the FileNotFound exception. Ironically it tells me it does not exist and gives me the complete path which I copy pasted in Windows explorer and it (WE) opened the file just file so it is not a question of the file not being in the wrong path or something. What could it be ?

    Code:
    using System;
    using System.IO;
    using System.Collections.Generic;
    
    namespace Utils
    {
    	class Logger {
    		private StreamWriter w;
    		
    		public Logger() {
    			w = new StreamWriter("log.txt", true);
    		}
    		
    		~Logger() {
    			w.Close();
    		}
    		
    		public void ToLog(string msg) {
    			w.WriteLine(System.DateTime.Now.ToString() + ": " + msg);
    		}
    	}
    	
    	class FSMachine {
    		private Dictionary<string, string> states;
    		
    		private void ParseLine(string line) {
    			char[] delimitors = {' ', '='};
    			string[] tmp = line.Split(delimitors);
    			states[tmp[0]] = tmp[1];
    		}
    		
    		public void CheckStatesFile(string path = "./config.ini") {
    			StreamReader r = new StreamReader(path);
    			while(r.Peek() >= 0) {
    				ParseLine(r.ReadLine());
    			}
    		}
    		
    		public FSMachine() {
    			CheckStatesFile();
    		}
    		
    		public bool IsStateSet(string key) {
    			return states.ContainsKey(key);
    		}
    		
    		public string GetState(string key) {
    			return states[key];
    		}
    	}
    }
    
    namespace BLABLABLA {
    	class Program
    	{
    		public static void Main(string[] args)
    		{
    			Utils.FSMachine fs = new Utils.FSMachine();
    			if(fs.IsStateSet("abc")) Console.WriteLine(fs.GetState("abc"));
    			Console.Write("Press any key to continue . . . ");
    			Console.ReadKey(true);
    		}
    	}
    }
    By the way, yes I am a hardcore noob in C#. I base all my code on my C++ experience and googling to know what to use in C# so please forgive my code if it is really stupid in some points.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It works fine once I created: C:\Documents and Settings\<user>\Local Settings\Application Data\Temporary Projects\cboard1\bin\Debug\config.ini

    If you are in Visual Studios, make sure you are putting it in the debug folder if you are actually debugging. You're going to have different folders depending on where or how you are building IRRC. Try specifying an absolute path instead:
    Code:
            public void CheckStatesFile(string path = @"c:\test\config.ini") {
                StreamReader r = new StreamReader(path);
                while(r.Peek() >= 0) {
                    ParseLine(r.ReadLine());
                }
            }
    Then it gets a different error later, but at least the path isn't the issue anymore.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User Samson144's Avatar
    Join Date
    Jun 2011
    Posts
    5
    I tried as Quzah said in y side and it works ok.

  4. #4
    Registered User
    Join Date
    Aug 2011
    Location
    Montreal, Quebec, Canada
    Posts
    73
    I found the error. It is because Windows added ".txt" after my "config.ini" making it "config.ini.txt". Changing the string to this in my code fixed this problem. The next problem was that I didn't initialize the Dictionary object. Now there is no error but the output is not what I want it to be so I will be working on this. Thank you

  5. #5
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    You need to set the working directory for paths to work both in the IDE and outside of it. In MSVS paths are relative to where your project files are and when you run an application files are relative to where the exe is.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help: end of file found before....
    By kamitsuna in forum C++ Programming
    Replies: 5
    Last Post: 02-24-2010, 02:23 PM
  2. Symbol file not found for *.ko
    By yanglei_fage in forum Linux Programming
    Replies: 3
    Last Post: 03-30-2009, 09:48 AM
  3. unexpected end of file found
    By eam in forum C++ Programming
    Replies: 5
    Last Post: 11-06-2003, 06:10 PM
  4. fatal error C1004: unexpected end of file found
    By Gutty in forum C++ Programming
    Replies: 1
    Last Post: 08-11-2003, 10:58 PM
  5. how do u fix unecpected end of file found???
    By mattyans in forum C Programming
    Replies: 4
    Last Post: 03-19-2002, 09:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21