|
-
December 12th, 2011, 08:52 AM
#1
File.Close()!!!
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?
-
December 12th, 2011, 12:56 PM
#2
Re: File.Close()!!!
It's automatically closed. What's the issue you're having? Are you trying to open it multiple times from multiple threads?
www.monotorrent.com For all your .NET bittorrent needs
NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.
-
December 12th, 2011, 12:59 PM
#3
Re: File.Close()!!!
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.
-
December 12th, 2011, 01:44 PM
#4
Re: File.Close()!!!
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.
Always use [code][/code] tags when posting code.
-
December 12th, 2011, 01:56 PM
#5
Re: File.Close()!!!
You can use FileStream (part of System.IO) to create new files provided you give a valid filename and creation conditions
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();
}
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.
-
December 12th, 2011, 04:23 PM
#6
Re: File.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.
Code:
if (Issue.Resolved)
{
ThreadTools.Click();
MarkThreadResolved();
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|