Windows Form Error: System.IO.FileNotFoundException: Could not find file

Last Reply 11 months ago By dharmendr

Posted 11 months ago

I create new files in each run manually by giving names of the files to save Ex: filename= "XYZ", file is saved as "XYZ.txt".

I combined the root path, and new filename to create. It gives me this error;

System.IO.FileNotFoundException: 'Could not find file 'C:\Users\Dell\source\repos\SaveReloadDeneme\SaveReloadDeneme\bin\Debug\Default'.'

at this line

string json2 = File.ReadAllText(path_combined);
    public partial class Form1 : Form
    {
        //FILENAME
        string input = Interaction.InputBox("Enter a serial number", "TEST", "Default", -1, -1);
        //DEFAULT PATH ROOT
        String root = @"C:\\Users\\Dell\\source\\repos\\SaveReloadDeneme\\SaveReloadDeneme\\bin\\Debug";

        Class1 c1 = new Class1();//holds method's attribute flags
        Class1 c2 = new Class1(); //holds method flags whether the used or not

        string path_combined;
        public Form1()
        {
            InitializeComponent();

            //default path + new filename
            path_combined = Path.Combine(root, input);



            //flag situation
            string json2 = File.ReadAllText(path_combined);
            FormatJson(json2);
            c1 = JsonConvert.DeserializeObject<Class1>(json2);


            //Method used or not
            string json = File.ReadAllText(path_combined);
            FormatJson(json);
            c2 = JsonConvert.DeserializeObject<Class1>(json);

        }

        private static string FormatJson(string json)
        {
            dynamic parsedJson = JsonConvert.DeserializeObject(json);
            return JsonConvert.SerializeObject(parsedJson, Formatting.Indented);
        }

I don't understand why, can you help me please?

Posted 11 months ago

 

Dan97 says:
string json2 = File.ReadAllText(path_combined);

 File.ReadAllText required file path not folder path.

So you are missing the file extension (.txt) in the path_combined. So change the code accordingly.