c# - StreamWriter not creating file? -


i wrote class outputs text file. worked quite well, , got job done. did same process, , didn't work (the file wasn't created). feel i'm missing simple - missing?

working code (in main method):

static void main(string[] args)     {         kinematics hello = new kinematics();          system.io.streamwriter file = new system.io.streamwriter             ("c:/users/myname/documents/googledrive/folder/folder/level 3.txt");         file.writeline("time(s)\tposition(m)\tvelocity(m/s)\tacceleration(m/s^2)");         //create file print stuff in.          file.close();         //close file.     } 

not working code:

class program {     static void main(string[] args)     {         system.io.streamwriter file = new system.io.streamwriter            ("c:/users/myname/documents/googledrive/folder/folder/hellooo.txt");         file.writeline("time(s)\tposition(m)\tvelocity(m/s)\tacceleration(m/s^2)");          projectile ballprojectile = new projectile();         ballprojectile.level1(file);         ballprojectile.level2(file);         ballprojectile.level3(file);          file.close();     } } 

the file paths same , both correct, , i've double checked looked in right place file.

kinematics calculations, , use file.writeline(); write out outputs of methods. writing done in main method. level1, level2, level3 methods empty methods of now. later running file.writeline(); methods.

move file.close() above

projectile ballprojectile = new projectile(); ballprojectile.level1(file); ballprojectile.level2(file); ballprojectile.level3(file);  

also, suggested in comments, streamwriter disposable , should doing this:

var file = new system.io.streamwriter(@"c:/users/myname/documents/googledrive/folder/folder/hellooo.txt")  using (file) {    file.writeline("time(s)\tposition(m)\tvelocity(m/s)\tacceleration(m/s^2)"); } 

note @ @ start of file path.


Comments