Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9022889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:37:46+00:00 2026-06-16T05:37:46+00:00

For some reasons, I want to write a GUID as ID into one file

  • 0

For some reasons, I want to write a GUID as ID into one file for save, and delete the id before opening the file.

 static void Main(string[] args)
    {
        string id = "b669fd8c904d48e0945c16cac1dc5ed9";
        byte[] idbyte = UnicodeEncoding.Default.GetBytes(id);

        FileStream input = new FileStream(@"C:\test.xlsx", FileMode.Open, FileAccess.Read);
        FileStream output = new FileStream(@"C:\test1.xlsx", FileMode.OpenOrCreate, FileAccess.Write);
        CopyStream(input, output);
        // add id with byte[] at the end of the file
        output.Write(idbyte, 0, idbyte.Length);
        output.Close();
        input.Close();

        FileStream input1 = new FileStream(@"C:\test1.xlsx", FileMode.Open, FileAccess.Read);
        FileStream output1 = new FileStream(@"C:\test2.xlsx", FileMode.OpenOrCreate, FileAccess.Write);
        int SizeOfBuffer = 1024 * 16;
        try
        {

            byte[] buffer = new byte[SizeOfBuffer];
            byte[] bufferLast = new byte[SizeOfBuffer];
            int read;
            int Nextread;
            while ((read = input1.Read(buffer, 0, buffer.Length)) > 0)
            {

                if ((Nextread = input1.Read(bufferLast, 0, buffer.Length)) > 0)
                {
                    output1.Write(buffer, 0, read);
                    output1.Write(bufferLast, 0, Nextread);
                }
                else   // delete id with byte[] 
                {

                    byte[] buffer2 = new byte[SizeOfBuffer];
                    for (int i = 0; i < read - idbyte.Length; i++)
                    {
                        buffer2[i] = buffer[i];
                    }
                    output1.Write(buffer2, 0, buffer2.Length);
                    break;
                }

            }


        }
        catch (Exception ex)
        {

            Console.WriteLine(ex.ToString());
        }
        output1.Close();
        input1.Close();
        Console.ReadLine();
    }

  private static void CopyStream(Stream input, Stream output)
    {
        int SizeOfBuffer = 1024 * 16;
        try
        {
            byte[] buffer = new byte[SizeOfBuffer];
            int read;
            while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
            {
                output.Write(buffer, 0, read);
            }
        }
        catch (Exception ex)
        {

            Console.WriteLine(ex.ToString());
        }
    }

But when I open the C:\test2.xlsx, excel shows error: “Excel found unreadable content in ‘test2.xlsx'”!

How to solve this problem ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T05:37:47+00:00Added an answer on June 16, 2026 at 5:37 am

    The following works for me. As you can see I did a bit of reorganizing so I could more easily tell what I was doing and to simplify the tests. Let me know if you have any questions.

    class Program
    {
        private const int SIZE_OF_BUFFER = 1024 * 16;
        private const string FILE_PATH_ORIGINAL = @"C:\test.xlsx",
                            FILE_PATH_WithId = @"C:\test1.xlsx",
                            FILE_PATH_IdRemoved = @"C:\test2.xlsx";
    
        private static readonly byte[] idbyte = UnicodeEncoding.Default.GetBytes("b669fd8c904d48e0945c16cac1dc5ed9");
    
        static void Main(string[] args)
        {
            AddGuidToFile(FILE_PATH_ORIGINAL, FILE_PATH_WithId);
            RemoveGuidToFile(FILE_PATH_WithId, FILE_PATH_IdRemoved);
            CompairFiles(FILE_PATH_ORIGINAL, FILE_PATH_IdRemoved);
    
            Console.ReadLine();
        }
    
        private static void AddGuidToFile(string sourceFilePath, string destinationFilePath)
        {
            byte[] buffer = new byte[SIZE_OF_BUFFER];
            FileStream input = new FileStream(sourceFilePath, FileMode.Open, FileAccess.Read),
                        output = new FileStream(destinationFilePath, FileMode.OpenOrCreate, FileAccess.Write);
            int read;
    
            try
            {
                while ((read = input.Read(buffer, 0, buffer.Length)) > 0)   //Copy source file to destination file.
                {
                    output.Write(buffer, 0, read);
                }
    
                output.Write(idbyte, 0, idbyte.Length);                     //Add guid to the end of the destination file.
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            finally
            {
                //Close files
                input.Close();
                output.Close();
            }
        }
    
        private static void RemoveGuidToFile(string sourceFilePath, string destinationFilePath)
        {
            byte[] currentBuffer = new byte[SIZE_OF_BUFFER],
                    nextBuffer = new byte[SIZE_OF_BUFFER],
                    tempBuffer;
            FileStream input = new FileStream(sourceFilePath, FileMode.Open, FileAccess.Read),
                        output = new FileStream(destinationFilePath, FileMode.OpenOrCreate, FileAccess.Write);
            int currentRead,
                nextRead = 0;                                       //Initialize to 0 incase file's lenght is less then SizeOfBuffer.
    
            try
            {
                currentRead = input.Read(currentBuffer, 0, currentBuffer.Length);   //Get first chunk.
                if (currentRead == currentBuffer.Length)                            //If first chunck is a full buffer then loop until it is not.
                {
                    nextRead = input.Read(nextBuffer, 0, nextBuffer.Length);
    
                    while (nextRead > idbyte.Length)
                    {
                        output.Write(currentBuffer, 0, currentRead);                //Since nextBuffer is large enough to contain the ID, write the current buffer out.
    
                        //Swap buffers.
                        tempBuffer = currentBuffer;
                        currentBuffer = nextBuffer;
                        nextBuffer = tempBuffer;
    
                        //Update counts and fill next buffer.
                        currentRead = nextRead;
                        nextRead = input.Read(nextBuffer, 0, nextBuffer.Length);
                    }
                }
    
                currentRead += nextRead - idbyte.Length;                            //Update currentRead to be the number of bytes in the buffer which are not part of the ID.
    
                output.Write(currentBuffer, 0, currentRead);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            finally
            {
                //Close files
                input.Close();
                output.Close();
            }
        }
    
        private static void CompairFiles(string originalFilePath, string idRemovedFilePath)
        {
            byte[] originalBuffer = new byte[SIZE_OF_BUFFER],
                    idRemovedBuffer = new byte[SIZE_OF_BUFFER];
            FileStream original = new FileStream(originalFilePath, FileMode.Open, FileAccess.Read),
                        idRemoved = new FileStream(idRemovedFilePath, FileMode.Open, FileAccess.Read);
            int originalRead,
                idRemovedRead;
    
            try
            {
                do
                {
                    originalRead = original.Read(originalBuffer, 0, originalBuffer.Length);
                    idRemovedRead = idRemoved.Read(idRemovedBuffer, 0, idRemovedBuffer.Length);
    
                    if (originalRead != idRemovedRead) { throw new Exception("Error: file sizes do not match.  originalRead = " + originalRead + ", idRemovedRead = " + idRemovedRead + ", SIZE_OF_BUFFER = " + SIZE_OF_BUFFER); }
    
                    for (int i = 0; i < originalRead; i++)
                    {
                        if (originalBuffer[i] != idRemovedBuffer[i]) { throw new Exception("Error: file contents do not match.  i = " + i + ",inputBuffer[i] = " + originalBuffer[i] + ", idRemovedBuffer[i] = " + idRemovedBuffer[i]); }
                    }
                }
                while (originalRead == idRemovedRead && originalRead > 0);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            finally
            {
                //Close files
                original.Close();
                idRemoved.Close();
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some security reasons I need to change apk file before user can download
I have a site at xyz.com, for some reasons I want to redirect access
What are some reasons why I wouldn't want to disable IIS logging completely for
So I've read some articles about scaling Socket.IO. For various reasons I don't want
My server for some reason displays .AIR file inside the browser, but I want
HellO, I want to write a binary file format viewer for windows which can
For performance reasons, i want to set the Indexed Attribute to some of my
I want to use ADO.net to extract some data from an Excel file. This
I'm using JSF 1.2. We want to write some selenium tests (based on xpath)
For some reasons, I'd like to write code like this: template<class T> class C

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.