Okay so this one should seem easy enough but I can't get past 1 issue I am having. Pretty much what ever is in the the listBox is stored in an array, but also in a text file (for loading and saving). The only issue is that I can't seem to get my remove to work correctly. I feel like I am missing something. Okay so I have a document that contains 1 word usually per line (these are shift types at a job). When the button is clicked to remove the certain item that is selected the program does the following

int loc = listBox1.SelectedIndex;

//Just a dialog box to make sure they want to remove the certain item
//---------------------------------------------------------------------------------------
string dialogDisplay = "Are You Sure You Want\nTo Remove " + listBox1.Items[loc].ToString() + " ?";
int qLoc = dialogDisplay.IndexOf('?');
      
qLoc = qLoc - 2;

dialogDisplay = dialogDisplay.Remove(qLoc, 1);
DialogResult result = MessageBox.Show(dialogDisplay, "Remove Shift?", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
//---------------------------------------------------------------------------------------


if(result == DialogResult.Yes)
 {
//shifts the array down to remove the item from the array
//---------------------------------------------------------------------------------------
  for (int i = loc; i < storedFileArray.Length - 1; i++)
   {
    storedFileArray[i] = storedFileArray[i + 1];
   }
//---------------------------------------------------------------------------------------

//Clears the file
//---------------------------------------------------------------------------------------
  FileStream input = new FileStream("shiftTypeSave.txt", FileMode.Create);
  input.Close();
//---------------------------------------------------------------------------------------

//Exports the array to the file
//---------------------------------------------------------------------------------------
  StreamWriter export = File.AppendText("shiftTypeSave.txt");    
     
  for (int i = 0; i < storedFileArray.Length; i++)
   {
    export.Write(storedFileArray[i]);
   }
  export.Write("\n");
  export.Close();   
//---------------------------------------------------------------------------------------

//reads the file back into the array (not sure why I did this, maybe to update the
//listBox, can't recall at the moment (not I reference to here in the next paragraph)
//---------------------------------------------------------------------------------------
  storedFileArray = readFile("shiftTypeSave.txt", 10); //10 is when end of line
  listBox1.DataSource = storedFileArray;
  listBox1.Refresh();
//---------------------------------------------------------------------------------------

Well from what I can tell this code works fine except for one part that has kept me stumpted for a good few hours and I just can't seem to get around it. If I remove the first item in the list box I run into a problem were the first item is a return statement. I placed MessageBox.Show's all around this code and found that this happened when I read back in the file I said above, so it has to deal with the exporting process. The thing is I don't know why the first line is a carriage return I mean I have tried everything, I hope this makes sense? Pretty much I want it so when the button is click, it removes the item from the array as well as the text document (so for example if they reopen this form the name will still be missing). Also this code interferes with a checkedListBox I have in another form (the last item on the list doesn't appear and the first item has no text, this worked fine before). Note that these properties will happen if I close the app and run it again (I opened the file in word pad and find that there is a blank line at the top followed by the rest of the lines are fine, but if I view the checkedListBox even after restarting the app they will only display the appropriate correct # of items, but second slotted text line will not show after the empty one, instead it skips to the third line). This is really pissing me off, as I have tried multiple ways the remove this carriage return (ascii code 10) but can't seem to and if I try to remove it using the programs remove button it does nothing. Please help me with this I am stumpted. Thanks in advance

Edited 6 Years Ago by JOSheaIV: Results differed, had a type because of it

Have you considered using File.ReadAllLines and File.WriteAllLines?
These automatically strip/add line end characters.

Edited 6 Years Ago by nick.crane: n/a

So your saying if I were to use WriteAllLines I could clear a file and then add all new stuff? I mean i'll try this when I can. (My laptop's GPU seems to be fried and I have no where to hook my desktop up at the moment)

bump (writeAllLine well I don't know what package to call). Another ideas as well

Well after a long night I figured it. I just had to make special expceptions for reading in the file and removing characters and then some other stuff

This question has already been answered. Start a new discussion instead.