CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: File.Close()!!!

  1. #1
    Join Date
    Nov 2011
    Posts
    38

    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?

  2. #2
    Join Date
    May 2007
    Posts
    1,546

    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.

  3. #3
    Join Date
    Nov 2011
    Posts
    38

    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.

  4. #4
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    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.

  5. #5
    Join Date
    Jun 2011
    Location
    Buenos Aires, Argentina
    Posts
    130

    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.

  6. #6
    Join Date
    Dec 2008
    Posts
    144

    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
  •  





Click Here to Expand Forum to Full Width

Featured