I need to close a file, then open a new one in my program, but I did not use StreamWriter to open the file. I used File.AppendAllText. How do I close this file because there is no File.Close() method?
Printable View
I need to close a file, then open a new one in my program, but I did not use StreamWriter to open the file. I used File.AppendAllText. How do I close this file because there is no File.Close() method?
It's automatically closed. What's the issue you're having? Are you trying to open it multiple times from multiple threads?
My program needs to gather data then email it out at midnight. once its emailed it needs to to open a new text file that collects data. How would I do that? I figured I could use some file.close method to close it then use file.appendalltext to a new text file.
As mentioned above it is closed automatically. To start using a new file you simply tell it to use a different filename, or you could move or delete the old file and allow it to continue using the same filename it's up to you.
You can use FileStream (part of System.IO) to create new files provided you give a valid filename and creation conditions
will create a new file (overwrite if it exists), write something to it and close it. Read more about the FileMode and FileAccess to adapt it to your needs.Code:folderPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + companyPath; // companyPath according to app assembly for example.
if (!Directory.Exists(folderPath))
Directory.CreateDirectory(folderPath);
string sOutputFilename = folderPath + [>yourFileName<];
if (!File.Exists(sOutputFilename))
{
FileStream fsOutputStream = new FileStream(sOutputFilename, FileMode.Create, FileAccess.Write);
fsOutpuStream.Write(...);
...
fsOutputStream.Close();
}
The thing you need to close is your FileStream object when you're done using it. Using statements automatically close them for you when the block is exited.