Click here to Skip to main content
15,940,550 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all

In my application i am creating reports in notepad file.
now what i want is if the file size is 2.4 KB then it should stop coping to that file, create a new file with same name and start writing there.

and later while printing i have to print all the records from all the files.

e.g. file1.txt
line1
line2
line3

file1_1.txt
line4
line5..


Please tell me how to do that.
Posted

Well you can create a file with specific size using FileStream.SetLength[^]

And after writing text to the different files, you can merge them like
store 1st file's text in some variable, 2nd file's text in some other variable and so on...
Then put all variables together and save a new file. Pretty Simple huh ! :)
C#
string path1 = @"D:\file1.txt";
string path2 = @"D:\file2.txt";
string newFilePath = @"D:\file3.txt";

string mergedText = System.IO.File.ReadAllText(path1);
mergedText += "\r\n";
mergedText += System.IO.File.ReadAllText(path2);

using (FileStream fs = new FileStream(newFilePath, FileMode.OpenOrCreate))
{
    System.IO.File.WriteAllText(newFilePath, mergedText );        
}


-KR
 
Share this answer
 
Comments
[no name] 4-Mar-14 23:55pm    
Good solution. :)
Krunal Rohit 4-Mar-14 23:56pm    
Thanks.

-KR
The problem you are facing make part by part.
First you have to write in a notepad file and then printing.

Writing notepad: calculate the counting or record the how amount of record is less then makes file size is 2.4 KB. So, always you will write only that amount of record. and if your record is more, then create a new file and write in new file.

Printing: You have to handle this in programmatically. I can see you are creating the file with a matching way like file1.txt, file1_1.txt. So, I think you can handle this with easily.

Start your work, If you face any problem then question us.
Thanks!
 
Share this answer
 
I'm give the basic logic here for better solution post your logic

string[] lines = System.IO.File.ReadAllLines(FilePath);
C#
for (int loop=0;loop<lines.count;loop++)>
{
    if(check file1 filesize<2.4)
    {
    //crete file1
    //Write file1   
    }
    else if(check file1 filesize>2.4)
    {
        //create new file2 and write it
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900