0

i search alot on google i found some response but didn't find what i want. what i want is suppose i have 5 files in a folder for the same date same date: 1st file-1mb 2nd file=2mb 3rd file-3mb 4th file-4mb 5th file-5mb

now when i click on my button it will delete all file except 5mb because of its size.

first i want to search files according to date in a folder and next delete all file for that particular date except the file which have the largest size.

i dont want code i want the functinality which can be used in this project. thanks in advance

amitesh
  • 766
  • 5
  • 17
  • 39
  • _"i search alot on google i found some response but didn't find what i want"_ - of course not, not every specific problem has been written out and solved on the web. You have to break your problem into small steps and solve them one by one, that is programming. In your question, I'm missing what you have tried. – CodeCaster Mar 21 '13 at 10:31
  • @CodeCaster i know that but i have work on it and that code in not good that's why i didn't posted here i don't want code i just know the process and i guess this question is ok so i don't think people just don't downvote that question sry if i say anything wrong – amitesh Mar 21 '13 at 10:53

1 Answers1

2

Using the DirectoryInfo you can get all the files in the specified path, Get the Max size file based on File.Length property and then delete like:

DirectoryInfo di = new DirectoryInfo("C:\"");
var files = di.GetFiles();
var maxFileSize = files.Max(r => r.Length);

foreach (FileInfo file in files.Where(r => r.Length < maxFileSize))
{
    file.Delete();
}
Habib
  • 219,104
  • 29
  • 407
  • 436
  • i have only one question it's very silly question i make a function and call that function on my button or i just put that code on my button event – amitesh Mar 21 '13 at 10:36
  • Its better if you extract that out to a function so that you can call it from multiple places. – Habib Mar 21 '13 at 10:40
  • @amitesh, how are you creating the path ? – Habib Mar 21 '13 at 10:55
  • DirectoryInfo di = new DirectoryInfo("E:\test"); that's my path – amitesh Mar 21 '13 at 10:58
  • 1
    @amitesh, you have to escape backslash, it should be `DirectoryInfo di = new DirectoryInfo("E:\\test")`, in string you have to escape backslash with double backslash – Habib Mar 21 '13 at 10:59
  • thanks habib it work can u tell me one more thing i ask in question that i want to delete it according to the date suppose i create 5 file in a day and 5 file yesterday in store them in same folder so there a problem will occur – amitesh Mar 21 '13 at 11:03
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/26624/discussion-between-amitesh-and-habib) – amitesh Mar 21 '13 at 11:03
  • @amitesh, sorry I can't come on chat, but you can select based on `CreationTime`, see http://stackoverflow.com/questions/9001544/sorting-a-listfileinfo-by-creation-date-c-sharp – Habib Mar 21 '13 at 11:09
  • @amitesh, I would love to, but I won't be here long. If its a new issue you can post question about it, with what you have tried and where you are stuck at, There are lots of people here on Stackoverflow, who would be able to help you out. Just remember to go through http://whathaveyoutried.com/ before posting a question. That will help you get better answers. – Habib Mar 21 '13 at 11:29