Note that there are some explanatory texts on larger screens.

plurals
  1. POAppend to file failure when executable not in same folder as data files
    primarykey
    data
    text
    <p>Problem is now solved. Mistake by me that I hadn't seen before.</p> <p>I am pretty new to coding in general and am very new to C# so I am probably missing something simple. I wrote a program to pull data from a login website and save that data to files on the local hard drive. The data is power and energy data for solar modules and each module has its own file. On my main workstation I am running Windows Vista and the program works just fine. When I run the program on the machine running Server 2003, instead of the new data being appended to the files, it just overwrites the data originally in the file.</p> <p>The data I am downloading is csv format text over a span of 7 days at a time. I run the program once a day to pull the new day's data and append it to the local file. Every time I run the program, the local file is a copy of the newly downloaded data with none of the old data. Since the data on the web site is only updated once a day, I have been testing by removing the last day's data in the local file and/or the first day's data in the local file. Any time I change the file and run the program, the file contains the downloaded data and nothing else.</p> <p>I just tried something new to test why it wasn't working and think I have found the source of the error. When I ran on my local machine, the "filePath" variable was set to "". On the server and now on my local machine I have changed the "filePath" to @"C:\Solar Yard Data\" and on both machines it catches the file not found exception and creates a new file in the same directory which overwrites the original. Anyone have an idea as to why this happens?</p> <p>The code is the section that download's each data set and appends any new data to the local file.</p> <pre><code>int i = 0; string filePath = "C:/Solar Yard Data/"; string[] filenamesPower = new string[] { "inverter121201321745_power", "inverter121201325108_power", "inverter121201326383_power", "inverter121201326218_power", "inverter121201323111_power", "inverter121201324916_power", "inverter121201326328_power", "inverter121201326031_power", "inverter121201325003_power", "inverter121201326714_power", "inverter121201326351_power", "inverter121201323205_power", "inverter121201325349_power", "inverter121201324856_power", "inverter121201325047_power", "inverter121201324954_power", }; // download and save every module's power data foreach (string url in modulesPower) { // create web request and download data HttpWebRequest req_csv = (HttpWebRequest)HttpWebRequest.Create(String.Format(url, auth_token)); req_csv.CookieContainer = cookie_container; HttpWebResponse res_csv = (HttpWebResponse)req_csv.GetResponse(); // save the data to files using (StreamReader sr = new StreamReader(res_csv.GetResponseStream())) { string response = sr.ReadToEnd(); string fileName = filenamesPower[i] + ".csv"; // save the new data to file try { int startIndex = 0; // start index for substring to append to file int searchResultIndex = 0; // index returned when searching downloaded data for last entry of data on file string lastEntry; // will hold the last entry in the current data //open existing file and find last entry using (StreamReader sr2 = new StreamReader(fileName)) { //get last line of existing data string fileContents = sr2.ReadToEnd(); string nl = System.Environment.NewLine; // newline string int nllen = nl.Length; // length of a newline if (fileContents.LastIndexOf(nl) == fileContents.Length - nllen) { lastEntry = fileContents.Substring(0, fileContents.Length - nllen).Substring(fileContents.Substring(0, fileContents.Length - nllen).LastIndexOf(nl) + nllen); } else { lastEntry = fileContents.Substring(fileContents.LastIndexOf(nl) + 2); } // search the new data for the last existing line searchResultIndex = response.LastIndexOf(lastEntry); } // if the downloaded data contains the last record on file, append the new data if (searchResultIndex != -1) { startIndex = searchResultIndex + lastEntry.Length; File.AppendAllText(filePath + fileName, response.Substring(startIndex+1)); } // else append all the data else { Console.WriteLine("The last entry of the existing data was not found\nin the downloaded data. Appending all data."); File.AppendAllText(filePath + fileName, response.Substring(109)); // the 109 index removes the file header from the new data } } // if there is no file for this module, create the first one catch (FileNotFoundException e) { // write data to file Console.WriteLine("File does not exist, creating new data file."); File.WriteAllText(filePath + fileName, response); //Debug.WriteLine(response); } } Console.WriteLine("Power file " + (i + 1) + " finished."); //Debug.WriteLine("File " + (i + 1) + " finished."); i++; } Console.WriteLine("\nPower data finished!\n"); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload