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! :)
 
Also, I put it into a class. Here are the 2 errors I got:
Error CS0246 The type or namespace name 'DirectoryInfo' could not be found (are you missing a using directive or an assembly reference?)
Error CS1579 foreach statement cannot operate on variables of type '?' because '?' does not contain a public definition for 'GetEnumerator'

Here is my the code that it's referring to (Sorry, I don't know if you can add the line numbers to the code on here):
C#:
using System;using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace Forge
{
    class Delete
    {
        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;
                    }


                }
            }
        }
    }
}
 
Here's a screenshot if that helps.
delete.png

I went into 'Project>Add Class' to make the class.
 
When you hover DirectoryInfo with the red squiggly line a lightbulb will appear and IDE will offer you suggestions for how to fix the problem. Two of those suggestions is to use the namespace DirectoryInfo class belongs to, either by importing the namespace or qualifying the usage inline. You can click the error correction option you want to apply.
 
When you hover DirectoryInfo with the red squiggly line a lightbulb will appear and IDE will offer you suggestions for how to fix the problem. Two of those suggestions is to use the namespace DirectoryInfo class belongs to, either by importing the namespace or qualifying the usage inline. You can click the error correction option you want to apply.

Thanks! I found out the class was missing
C#:
using System.IO;

Now all I have to figure out is where to insert the data like the folder path (C:\SysApp), the cache limit (150000KB), and how much to delete at a time (149900KB). :joyous:
 
Last edited:
Back
Top Bottom