Saturday, 15 February 2014

c# - create textfile dynamically if textfile size exceeds maximum size -



c# - create textfile dynamically if textfile size exceeds maximum size -

i'm writing text files using streamwriter using next code:

path == @"desktop\"; filepath1 = path + "1.txt"; streamwriter _sw = new streamwriter(filepath1, true); _sw.writeline("some text"); _sw.close();

if size of textfile exceeds 500kb want create text files dynamically. i'm tryng next code:

var size = (path.length)/1024; if(size>=500) { int = (size/500)+1; var filepath2 = path + + ".txt"; if (file.exists(filepath2)) { streamwriter _sw = new streamwriter(filepath2, true); _sw.writeline("some message"); _sw.close(); } } else { filestream fs = file.create(filepath2); streamwriter _sw = new streamwriter(filepath2, true); _sw.writeline(ex); _sw.close(); }

my question if file 2.txt exceeds 500kb want create 3.txt,4.txt..... , on.. want create these dynamically - how solve problem?

where start..

you writing 1 big long procedural script. need break downwards chunks can reused using functions. is, out of command way quickly.

path == @"desktop\"; not valid. 1 many =

use path.combine() combine folder , filenames.

i'm sure test/rough/scratch code in case it's not, check out try/except wrap file handling. should using() dispose of streams/writers.

my lastly comment see lot of sort of code lot , it's replaceable nlog whole lot less friction.

i have commented login has no rep.

c# asp.net .net

No comments:

Post a Comment