c# - StreamWriter not appending to file, instead creating a new file in different directory -


i attempting write 1 file temp file after making changes of data in source file. working until extracted logic own function.

after running tests noticed first line being saved file in location specified. after debugging able see second call function , onward file location in different location.

i have 2 questions, first - why happening? second - best way make sure gets fixed , not happen again?

str[] file broken delimiter 1 line @ time. filename name of specific file contained in directory of files (this program loops through number of files).

public static void writefile(string[] str, string filename) {     rowcount++;     using (streamwriter sw = new streamwriter(filename.substring(0, filename.length - 4) + "_tmp.txt", true, system.text.encoding.unicode))     {         string tempstring = string.empty;         (int = 0; < str.length; i++)         {             if (ismrt)             {                 tempstring += str[i] + ",";             }             else             {                 tempstring += str[i] + "|";             }         }         logger.logmessage("writting line file: " + "row #: " + rowcount + " ,file path: " + filename.substring(0, filename.length - 4) + "_tmp.txt");         sw.writeline(tempstring);     } } 

edit: directory , list of files , function calls write method

static string directory = configurationmanager.appsettings["directory"]; static list<string> filenames = directory.getfiles(directory).tolist();  private static void activitycheck(string filename, string[] alllines)         {             foreach (string nextline in alllines.skip(1))             {                 string[] templine;                 if (ismrt)                 {                     templine = nextline.split(',');                 }                 else                 {                     templine = nextline.split('|');                 }                 logger.logmessage("checking activity flag...");                 if (templine[count].length == 6)                 {                     checkcolumn(templine);                     logger.logmessage("activity flag ok");                     writefile(templine, filename);                 }                 else if (templine[count].length > 6)                 {                     templine[count] = templine[count].trim();                     checkcolumn(templine);                     logger.logmessage("fixed activity flag");                     writefile(templine, filename);                                     }                 else                 {                     logger.logmessage("the activity flag did not have enough numbers in row #: " +rowcount);                     //throw new exception("there not enough numbers in activity flag");                 }     }         } 

edit2: newly made temp file gets created in visual studio folder in bin folder of project. not sure if useful has maybe run before.

i have found solution problem. passing in name of file , not whole path. of course missed 1 function call still passing in name of file , not path leading temp file being created in project directory.

was quick fix after finding it:

activitycheck(nameoffile, alllines); 

to

activitycheck(pathtofile, alllines); 

if finds files being created in project/bin/debug location , know set destination directory correctly, double check passing actual path.


Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -