none
How do I update a already existing data in a file using filestreams RRS feed

  • Question

  • For example I created an insert function using filestream operations and it got saved to a file and now I want to update some of the details in the already saved file how can I do this?

    Sunday, May 5, 2019 10:16 AM

All replies

  • Please show the current code so those who may help have an idea of where you are and also all or some of the file contents would help too.

    Without that imagine you reading this post and what would you ask for clarification?


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Sunday, May 5, 2019 10:55 AM
    Moderator
  • Hi PanthuShetty,

    Thank you for posting here.

    For your question, a simple is to read the stream first and then clean the file to save the new stream.

    It would be better to provide you code, we will try our best to give you some suggestions.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Sunday, May 5, 2019 2:56 PM
    Moderator
  • If you know the position, then check this example:

    using( var fs = new FileStream( @"MyFile.bin", FileMode.Open, FileAccess.ReadWrite, FileShare.None ) )
    {
        fs.Position = 1234;
        fs.Write( someNewBytes, ... );
    }
    

    Sunday, May 5, 2019 5:15 PM
  • For example I created an insert function using filestream operations and it got saved to a file and now I want to update some of the details in the already saved file how can I do this?

    Keep in mind that any attempt to alter a file "in place" (without recreating it)
    generally is limited to overwriting an exact number of characters. There is no
    provision for inserting or removing characters directly on disk. Those operations
    typically require reading the file into memory, altering it in memory, and then 
    rewriting it to disk. 

    Or by creating a new copy of the file by reading and writing up to the point 
    where it is to be changed, writing new data to the file or skipping characters
    from the input, then writing the remainder from the input file. After copying 
    with revisions is complete, the original file can be renamed as a backup (or
    deleted) and the copy renamed as the original.

    - Wayne

    Sunday, May 5, 2019 10:30 PM