-
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.
-
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.
-
I tried as Quzah said in y side and it works ok.
-
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 :)
-
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.