Make a new project in Visual Studio (Console App)
Add a new .cs file named as MySetting.cs, in that file copy paste the following code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; namespace file_handling { class MySettings { public int MyNumber { get; set; } public string MyData { get; set; } public void Save() { Stream stream = File.Create(SettingsFile); StreamWriter writer = new StreamWriter(stream, Encoding.UTF8); writer.WriteLine(MyNumber); writer.WriteLine(MyData); writer.Close(); stream.Close(); } public static MySettings Load() { if (!File.Exists(SettingsFile)) return DefaultSetting; Stream stream = File.OpenRead(SettingsFile); StreamReader reader = new StreamReader(stream, Encoding.UTF8); string firstline = reader.ReadLine(); string secondline = reader.ReadLine(); reader.Close(); return new MySettings { MyNumber = int.Parse(firstline), MyData = secondline }; } private static string SettingsFolder { get { string folder = "C:\\File Handling"; folder = Path.Combine(folder, "my company"); folder = Path.Combine(folder, "my app"); if (!Directory.Exists(folder)) Directory.CreateDirectory(folder); return folder; } } private static string SettingsFile { get { return Path.Combine(SettingsFolder, "Settings.txt"); } } private static MySettings DefaultSetting { get { return new MySettings { MyNumber = 0, MyData = " Harshit Rastogi ", }; } } } } //In the program.cs file add the following code. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; namespace file_handling { class Program { static void Main(string[] args) { Console.WriteLine("Loading settings"); MySettings settings = MySettings.Load(); Console.WriteLine("My Number ={0}", settings.MyNumber); Console.WriteLine("My data ={0}", settings.MyData); Console.WriteLine(); Console.WriteLine("upadating settings and printing at next run"); settings.MyNumber++; settings.MyData += "\n " + DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString() + " " + DateTime.Now.Millisecond.ToString() + " ms "; settings.Save(); Console.WriteLine("done"); Console.ReadLine(); } } } |
Yes you are done with file handling reading and writing.
Here in above code we are making a new folder if it doesn't exist and if it exist we are using the content in the file to display on console screen, very easy.
Put a breakpoints, run and debug.
If you like this post, please subscribe us
Comments
Post a Comment
Important - Make sure to click the Notify Me check-box below the comment to be notified of follow up comments and replies.