2

I am fairly new to C# and would like to ask how can I set a certain file limit to a comma-separated file. The scenario that I would be using them is as follows.

  1. Before I write the CSV output file, I need to check if there is an existing CSV file or not on a specific path/drive
  2. While the method is running, how can I make it stop writing before it exceeds its file size limit (preferably 500 MB worth of file size of CSV)?

This is currently the code I am using:

if (!File.Exists(newFileName))
{
    File.WriteAllText(newFileName, clientheader);
    //File.WriteAllText(logFileName, logfileheader);
}
else
{
    File.AppendAllText(newFileName, clientheader);
    // File.AppendAllText(logFileName, logfileheader);
}
Chris Mantle
  • 6,595
  • 3
  • 34
  • 48
Eman
  • 141
  • 2
  • 6
  • 2
    Look at `File.Exists` for the first part of your question: http://msdn.microsoft.com/en-us/library/system.io.file.exists(v=vs.110).aspx. In terms of keeping track of how much data you've written, a simple approach is to keep a count of how many characters you have written, as each character is two bytes. – hunch_hunch Sep 17 '14 at 17:01
  • I would want to put the file size limit globally. would that be okay? – Eman Sep 17 '14 at 17:03
  • 1
    What code do you have already for writing the CSV file? Start by adding that to your question. – Matt Burland Sep 17 '14 at 17:03
  • How are you creating the CSV file? If you are manually creating it in your code then you simply need to track the amout of written data and stop at 500MB. If you are using some 3rd party tool then you need to look at that API. – Gridly Sep 17 '14 at 17:04
  • This is currently the code i am using if (!File.Exists(newFileName)) { File.WriteAllText(newFileName, clientheader); //File.WriteAllText(logFileName, logfileheader); } else { File.AppendAllText(newFileName, clientheader); // File.AppendAllText(logFileName, logfileheader); } } – Eman Sep 17 '14 at 17:06
  • 2
    @Noelskie, add the code you're using to your original question instead of in the comments. – hunch_hunch Sep 17 '14 at 17:06
  • 1
    See here: http://stackoverflow.com/questions/8707755/how-to-know-the-size-of-the-string-in-bytes to figure out the length of your string in bytes. – Matt Burland Sep 17 '14 at 17:10

2 Answers2

3

You can use a stream (FileStream) for writing the csv data and check the current size by checking the Position property of the stream. Let's suppose you have data in the allData array that you want to write to a csv file (for example Name, Age, etc), then it should work like this:

const int sizeLimitInBytes = 500*1024*1024;

using (var stream = new FileStream("data.csv", FileMode.Append))
using (var writer = new StreamWriter(stream))
{
    // if the file is newly created then write the csv column names in the first line
    if (stream.Position == 0) 
    {
       writer.WriteLine("Name, Age, Job, Address");
    }

    foreach (var currentData in allData)
    {
        if (stream.Position > sizeLimitInBytes)
        {
            break;
        }
        writer.WriteLine("..."); // write data seperated by commas
    }
}

Use FileMode.Append to write to the end of the file if it exists. Otherwise it will create one.

StreamWriter uses buffering (with a 4 kilobyte buffer size), so checking whether the limit was exceeded won't be completely accurate, but for a 500 MB limit it doesn't really matter.

If you prefer to write all contents once to the file then you can use a MemoryStream instead of a FileStream, and then use File.WriteAllBytes to dump the bytes of the stream to the file.

SAm
  • 2,154
  • 28
  • 28
Bedford
  • 1,136
  • 2
  • 12
  • 36
0

I had a similar problem, a file was created with only limited characters. I found the solution and I tried "using " keyword while creating the file. Now the file is created with the maximum size.

using(StreamWriter sw = new StreamWriter(fs))
{
    ArrayList chartList = GetChart(maintNode);    
    foreach (var line in chartList)
    {
        sw.WriteLine(line);
    }
}

From: StreamWriter limit in C# in text file

Marcello B.
  • 4,177
  • 11
  • 45
  • 65
  • Can you elaborate this answer a little bit more. I don't understand the answer based on what you provided. Could you give an example of what you did to solve it. – spijs Oct 09 '18 at 20:28
  • This answer doesn't address the OP's question. – Erick Brown Feb 23 '21 at 18:03