user3347005
user3347005

Reputation: 23

create textfile dynamically if textfile size exceeds maximum size

I'm writing text to files using StreamWriter using the following code:

path == @"Desktop\";
filepath1 = path + "1.txt";
StreamWriter _sw = new StreamWriter(filepath1, true);
_sw.WriteLine("some Text");
_sw.Close();

if size of textfile exceeds 500kb I want to create text files dynamically. I'm tryng following code:

var size = (path.Length)/1024;

if(size>=500)
{
    int i = (size/500)+1;
    var filepath2 = path + i + ".txt";

    if (File.Exists(filepath2))
    {
        StreamWriter _sw = new StreamWriter(filepath2, true);
        _sw.WriteLine("Some message");
        _sw.Close();
    }
}
else
{
    FileStream fs = File.Create(filepath2);
    StreamWriter _sw = new StreamWriter(filepath2, true);
    _sw.WriteLine(ex);
    _sw.Close();
}

My question is if file 2.txt also exceeds 500kb I want to create 3.txt,4.txt..... and so on.. I want to create all these dynamically - how to solve this problem?

Upvotes: 1

Views: 2815

Answers (2)

H. Mahida
H. Mahida

Reputation: 2366

First thing you need to do the SIZE comparison for the data length of File not the File Path.

Here is Function which dose what you want to achieve, Please make appropriate changes for your path.

     //Public variable to manage file names
    int FileCounter = 1;

    string FileName;

    // Call this function to Add text to file
    private void WriteToFile(string writeText)
    {

        FileName = "MyFile_"+FileCounter +".txt";
        if (File.Exists(FileName))
        {
            string str = File.ReadAllText(FileName);

            if ((str.Length + writeText.Length) / 1024 > 500)  // check for limit
            {
                // Create new File
                FileCounter++;
                FileName = "MyFile_" + FileCounter + ".txt";
                StreamWriter _sw = new StreamWriter(FileName, true);
                _sw.WriteLine(writeText);
                _sw.Close();

            }
            else  // use exixting file
            {
                StreamWriter _sw = new StreamWriter(FileName, true);
                _sw.WriteLine(writeText);
                _sw.Close();
            }
        }

    }

Upvotes: 1

nevin
nevin

Reputation: 1

Where to start..

You are writing it as one big long procedural script. You need to break it down into chunks that can be reused using functions. As it is, it will get out of control way too quickly.

path == @"Desktop\"; is not valid. 1 too many =

Use Path.Combine() to combine your folder and filenames.

I'm sure this is all just test/rough/scratch code but just in case it's not, also check out Try/Except to wrap your file handling. You should also look up using() to dispose of your streams/writers.

My last comment would be that I see a lot of this sort of code a lot and it's often replaceable with something like Nlog for a whole lot less friction.

I would have commented but this login has no rep.

Upvotes: 0

Related Questions