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 7605895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:16:56+00:00 2026-05-31T00:16:56+00:00

I have this code: private void SaveStreamToFile(string fileFullPath, Stream stream) { if (stream.Length ==

  • 0

I have this code:

private void SaveStreamToFile(string fileFullPath, Stream stream)
    {
        if (stream.Length == 0) return;

        // Create a FileStream object to write a stream to a file
        using (FileStream fileStream = System.IO.File.Create(fileFullPath, (int)stream.Length))
        {
            // Fill the bytes[] array with the stream data
            byte[] bytesInStream = new byte[stream.Length];
            stream.Read(bytesInStream, 0, (int)bytesInStream.Length);

            // Use FileStream object to write to the specified file
            fileStream.Write(bytesInStream, 0, bytesInStream.Length);
        }
    }

I call this method like this SaveStreamToFile(@”f:\Test.txt”, memoryStream);

I got error:File operation not permitted. Access to path ‘f:\Test.txt’ is denied.

  • 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-05-31T00:16:58+00:00Added an answer on May 31, 2026 at 12:16 am

    Well, presumably that means you don’t have write access to f:\Test.txt. That’s a problem outside the scope of .NET, really.

    However, your method is broken. Here:

    byte[] bytesInStream = new byte[stream.Length];
    stream.Read(bytesInStream, 0, (int)bytesInStream.Length);
    

    you’re assuming that you can get the length of the stream (not all streams support this), and you’re also assuming that Read will read the whole thing in one go. That’s not necessarily the case.

    If you’re using .NET 4, you can use Stream.CopyTo, which will make life a lot simpler. (Although that won’t help you to abort if there’s no data in the stream.) But you’ll still need to fix the inability to write to f:\Test.txt to start with.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code private void writeReport(IReport report, string reportName) { string reportString =
I have this code: private static void saveMetricsToCSV(String fileName, double[] metrics) { try {
I have this code: private void ModifyButton_Click(object sender, RoutedEventArgs e) { ModifyButton.Content = Another
i have this code: private void aboutToolStripMenuItem_Click(object sender, EventArgs e) { this.WindowState = FormWindowState.Minimized;
hello atm I have this code private void checkBox1_CheckedChanged(object sender, EventArgs e) { if
I have this code: private void WheelResize(object sender, MouseWheelEventArgs e) The compiler gives me
I currently have this type of code: private void FillObject(Object MainObject, Foo Arg1, Bar
I have this code: void wait(int ms) { System.Threading.Thread.Sleep(ms); } private void button1_Click(object sender,
I have this code : private void vicationDataGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { if (zawag)
I have this code attached to LostFocus and Deactivate events: private void Window_LostFocus(object sender,

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.