using (var sr = new StreamReader(Misc.tempFile))
				{
					using (var sw = new StreamWriter(Misc.path + "test.dat"))
					{
						while ((line = sr.ReadLine()) != null)
						{
							for (int i = 0; i < detailViewer.SelectedIndices.Count; i++)
							lineToDelete[i] = Encryption.encrypt(detailViewer.SelectedItems[i].SubItems[0].Text) +
							"," + Encryption.encrypt(detailViewer.SelectedItems[i].SubItems[1].Text) + ","
							+ Encryption.encrypt(detailViewer.SelectedItems[i].SubItems[2].Text);
							if (line != lineToDelete[detailViewer.SelectedIndices.Count - 1] && !line.StartsWith("/"))
							sw.WriteLine(line);
						}
					}
				}

It works fine but it doesn't delete all the lines that need to be deleted, I've tried using a loop on the line check, but all that does it duplicate the lines. Please help me.

Sorry about the conventions, for some reason they mess up when I paste code snippets here.

Thanks.

Recommended Answers

All 8 Replies

You need to remove some of the white space when you post your code. If you use VS, select the code you wish to post then press Shift + Tab to move it one tab closer to the left margin. Do this to take out the extra indents before posting it, it makes it more readable :)

For some reason I can't edit my post, so I'll create a new topic.

As for your code, you are looping through each of the SelectedIndices and building a line to store in lineToDelete. But when you test the current line from teh file you only test it against the last linesToDelete item : line != lineToDelete[detailViewer.SelectedIndices.Count - 1].
I think you might want to reconsider your logic. Im assuming you want to only print the file lines to the new file if they are not in your SelectedIndices collection. At present you are looping through the SelectedIndices and rebuilding your linesToDelte collection for every line in the file. Unless the linesToDelete colelction is going to change, you should populate it once outside the read/write process and then jsut check the current line against it.
In pseudocode, something like:

  1. Create collection of linesToDelete
  2. For each selected item in detail viewer, add item to linesToDelete

  3. Open file 1

  4. Read line from file 1
  5. Compare current line from file 1 to each line in LinesToDelete
  6. if no match is found, write line to file 2

repeat 4 - 6 for each line in file

I'd suggest using a boolean to determine if a match is found, something like:

bool matchFound = false;
if(line == linesToDelete[index])
{
    matchFound = true; //store that you found a match
    break; //break stops the current loop..
           //stops you checking the rest of the lines since you found a match
}

I'm about to shoot somebody, nothing is working.

ok..take a deep breath.
What changes have you made and what "isnt working"?
Have you tried altering your logic based on the pseudocode i gave you?

I think this should working:

using (var sr = new StreamReader(Misc.tempFile))
{
  using (var sw = new StreamWriter(Misc.path + "test.dat"))
    while ((line = sr.ReadLine()) != null)
    {
      bool skip = false;
      for (int i = 0; i < detailViewer.SelectedIndices.Count; i++)
      {
        lineToDelete = Encryption.encrypt(detailViewer.SelectedItems[i].SubItems[0].Text) +
"," + Encryption.encrypt(detailViewer.SelectedItems[i].SubItems[1].Text) + ","
+ Encryption.encrypt(detailViewer.SelectedItems[i].SubItems[2].Text);
        if (line == lineToDelete && line.StartsWith("/"))
        {
          skip_line = true;
        }
      }
      if(!skip_line)
      {
        sw.WriteLine(line);
      }
    }
  }
}

Yup..that will probably work, MarcLight, although here at daniweb the general concensus is that its better to guide people to the right answer rather than give it to them. Give them a fish and all that :p

@Jaydenn, i strongly advise you to examine the code and figure out why it works. If you copy and paste it and move on you wont know where you were going wrong and will make the same mistakes again in the future.

Thanks for the help guys. @Marc, your method worked like a charm. I love you deeply.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.