How will delete the file using c#?
I want to delete file using the delete function.
foreach (string file1 in filePaths)
{
file = Path.GetFileName(file1);
while (reader.Read())
{
client = reader["name"].ToString();
filename = reader[2].ToString();
if (filename != file)
flag = 1;
else
flag = 0;
}
if (flag == 1)
{
sw.WriteLine(file);
File.Delete(file);
开发者_如何学Go
data_count++;
}
}
My file is not deleting.
Well, you haven't given nearly enough information to really let us help you properly. What is your code meant to be doing?
I suspect that the problem is that your flag
variable (which looks like it should probably be of type bool
rather than int
) is entirely dependent on the last iteration of your while loop. Basically your code currently says: "Delete the file if the final record in the reader talks about a different file."
Is that what you wanted it to say?
Note that the first iteration of the foreach
loop is going to read from reader
to completion... subsequent iterations will never read any more data, and indeed will use the existing value of flag
. So you'll either end up deleting all the files or none of them. Again, I doubt that's what you actually want to do.
I suspect you should actually be reading from reader
in one loop, building up a set of filenames, and then go through your "candidate" files in another loop. But without more information, it's hard to say for sure.
精彩评论