Cache Limit?

ReiSixx9

Member
Joined
Dec 11, 2016
Messages
14
Programming Experience
Beginner
I want to set a cache limit for my c# program. My program is creating files periodically and saving them to a folder. I want it so if the folder hits this limit (i.e. 1GB) it will automatically start deleting the files starting with the oldest ones deleting only a certain amount at a time (i.e. 500MB).
How do I do this? I'm using Visual Studio Community 2015.
Thanks! :)
 
E.g.
Private Sub DeleteOldFilesIfOverFolderLimit(folderPath As String,
                                            folderSizeLimit As Long,
                                            amountToDelete As Long)
    Dim folder As New DirectoryInfo(folderPath)
    Dim files = folder.GetFiles()
    Dim folderSize = files.Sum(Function(fi) fi.Length)

    If folderSize > folderSizeLimit Then
        'Sort the list of files with the oldest first.
        Array.Sort(files,
                   Function(fi1, fi2) fi1.CreationTime.CompareTo(fi2.CreationTime))

        Dim amountDeleted = 0L

        For Each file As FileInfo In files
            amountDeleted += file.Length
            file.Delete()

            If amountDeleted >= amountToDelete Then
                Exit For
            End If
        Next
    End If
End Sub
 
Sorry, I'm really new to this. Can I just copy paste this code into my script or do I have to customize part of it? If so, what part(s)? Also, what area of my script should I place this in?
 
Sorry, I'm really new to this. Can I just copy paste this code into my script or do I have to customize part of it? If so, what part(s)? Also, what area of my script should I place this in?

That is far more code than I usually provide as it is. We're not here to be your code-writing servants. If you want to learn how to code in C# then do so. Getting other people to write code that you can just copy and paste is not the way to do that. You posted six minutes after I did so you clearly have made no effort at all to understand what I provided. If you're not prepared to make any effort, I'm not either. I'll help those who help themselves but not those who just want their work done for them.
 
I'm sorry. I've been working on trying to figure it out for myself, but I can barely read normal text. I'm recovering from a brain tumor so it can get a bit tricky. I just don't quite understand your format. If you could just help me figure out which parts of the code to customize so I can try and figure out the rest that would be amazing. Sorry once again, I really didn't mean to ask for you or anyone to do my work for me. Thank you.
 
Why do you think you need to change anything? That code will work as is, assuming that you pass it meaningful data. What exactly do you not understand about it and what efforts have you made to do so?

For instance, I would think that the method parameters should be relatively self-explanatory, but we'll get back to them. The first line create a DirectoryInfo. Do you know what that is and how it works? If so then that's that line out of the way. If not then what have you done to find out? Have you used the Help menu in VS to open the MSDN documentation and searched for the topic on that type? Have you Googled it?

The problem I have is that many people think they've tried when they haven't really. Looking at the code and thinking really hard is not really going to cut it. I don't necessarily expect people to understand everything but I do expect that they will make reasonable efforts to do so and then be specific about the parts that they can't.

For instance, I can quite accept that that call to Array.Sort might be confusing in its specifics but that's why I put a comment on that line specifically. You don't have to know exactly how it works though. The comment tells you what it does and that's enough. If you really want to understand it though, you can simply use the F1 key in VS to open the relevant documentation directly. That is what I expect people to do when I give them information like this. I expect them to use the resources available to them to find out more. I am then more than willing to help with further specific issues but "I don't understand" reads as "I'm not willing to research" to me. I'm sorry about your health issues but I'm still not a code-writing service.
 
I think I'd need to change some things in the code because I've noticed that usually codes provided on the internet for this sort of thing need customization to fit my specific situation. (Sorry if that made no sense. My brain just messed up massively and could not think of the correct words to use.)

That code will work as is, assuming that you pass it meaningful data.
What do you mean 'pass it meaningful data'?

I tried Googling the code at first just to give it a shot although I didn't have high hopes of it returning anything. I also asked my fiance if he had any ideas, but he knows absolutely nothing of this kind of thing. And I tried reading it out loud repeatedly (that's the only way I can try and read articles and books and such otherwise it just goes over my head). Finally, I tried taking apart the code and examining it's parts so that maybe I could guess which parts needed to be changed to fit my situation. None of this worked, unfortunately.

DirectoryInfo? I Googled it and tried to understand as much of this page as I could. I get what it is now, but I only kind of get how it works. I actually didn't know VS had a Help menu that could give me that info. Thanks for letting me know, I'll use it as much as I can.


Sorry, I'm so used to comments being in the form of //___ if that makes sense. I guess that's one of the big problems I had reading the code.

I also just realized that this code is in visual basic.net, correct?
 
Oh, think I owe you a big apology. I just realised that I posted VB code and this is the C# forum. I spend most of my time in VB forums and I sometimes do forget and post in the wrong language. That explains what you meant when you said that you didn't understand the format. I really am sorry for that confusion. Here's the C# equivalent:
private void DeleteOldFilesIfOverFolderLimit(string folderPath,
                                             long folderSizeLimit,
                                             long amountToDelete)
{
    var folder = new DirectoryInfo(folderPath);
    var files = folder.GetFiles();
    var folderSize = files.Sum(fi => fi.Length);

    if (folderSize > folderSizeLimit)
    {
        // Sort the list of files with the oldest first.
        Array.Sort(files,
                   (fi1, fi2) => fi1.CreationTime.CompareTo(fi2.CreationTime));

        var amountDeleted = 0L;

        foreach (var file in files)
        {
            amountDeleted += file.Length;
            file.Delete();

            if (amountDeleted >= amountToDelete)
            {
                break;
            }

        }
    }
}
It's after 1 AM here so I can't go into any more detail now but I will tomorrow if you've had a go but are still having trouble. As I said though, the code (now that it's the right language) should just work as is if you pass it meaningful data. That would mean the full path of the folder and, according to your original example, the numbers of bytes in 1 GB and 500 MB. If you then want to change it to accept a number of MB, for instance, then you can do that and add the appropriate arithmetic to the method, i.e. multiply by 1024 if the parameter is a number of KB, multiply by 1024*1024 for MG, 1024*1024*1024 for GB and so on.
 
Awesome! Thank you sooo much. I have just one more question. What part of the script to I put this code into? The Manifest? If so, under what "section"?
 
Never mind. I think I've figured that out. I'm just still stuck on which exact parts of the code to change.

Here's what I have so far of what I think might be right (I couldn't figure out how to add code on here so I made a link, sorry.)
Red, I think is where I put the full path of the folder (C:\SysApp).
Yellow, I think is where I put the folder size limit (150000KB).
Green, I think is where I put the amount to delete (149900KB).

I think the code should look like this going by what I have so far, but it's most likely really far off.

Could you possibly help me figure out this last bit please? :)
 
I'm just still stuck on which exact parts of the code to change.
As I have already said, you don't need to change anything, as long as you pass meaningful data. "Meaningful data" means data that means something reasonable. For instance, if you*don't the path of a folder that exists then that's not meaningful. If you pass a negative folder size limit then that's not meaningful. Nothing magical. Just sensible stuff.
I couldn't figure out how to add code on here so I made a link, sorry.
You simply*copy the code form your IDE and paste it into your post, between appropriate formatting tags. You can either type the tags by hand or use the toolbar on the advanced editor. The XCODE tags are best for code but just make sure that you provide the C# option when prompted. If you want to provide extra formatting, e.g. bold or coloured text, then use CODE tags.

As for where to put the code, it's up to you. Put it somewhere that makes sense. Where do you want to use this code? If it's just*in the main form of your project then that's where you'd put the code. As for calling the code,*it's a method like any other so you call it like any other method, passing the appropriate arguments. As you say - and as the names and usage suggest, the method expects the*path of the folder you want to process, the maximum size in bytes that you want to allow the folder to grow to and the minimum amount of file data in bytes you want to delete if the folder grows beyond that limit.
 
This happens when I try and use the code. (Sorry, I don't know how to get rid of the original picture I accidentally posted. Just look at the embedded picture and not the attached one.)
code1.png
 
I'm just really having trouble trying to figure out where in the code to insert the meaningful data. If you could highlight the parts and explain it to me that would be amazing. :)
 
It's a method. Methods have to be inside a class. You have pasted that code into the Program.cs file but outside the Program class. Any time we post advice here, we should be able to assume that you know the basics of where to put a method in your code. If you don't know that sort of thing then you need to spend some time learning the fundamentals. If someone asks me to teach them how to drive, I don't want to spend my time showing them how to open the door and put on their seatbelt. That's stuff they should already know and spend their own time to learn if they don't.
 
It's a method. Methods have to be inside a class. You have pasted that code into the Program.cs file but outside the Program class. Any time we post advice here, we should be able to assume that you know the basics of where to put a method in your code. If you don't know that sort of thing then you need to spend some time learning the fundamentals. If someone asks me to teach them how to drive, I don't want to spend my time showing them how to open the door and put on their seatbelt. That's stuff they should already know and spend their own time to learn if they don't.

Oh sorry about that, earlier on you said I could put it aywhere so I must've misunderstood.


Could you possibly just help me figure out where in the code to insert the "meaningful data"?
 
Back
Top Bottom