locked
[U8.1]what is the best way to save a stream to a file? RRS feed

  • Question

  • I can't decide which is better way among the below 2 methods to save a stream to a file in c# 

    Method 1: 
    int i = 0;
                var bufflist = new List<byte>();
                stream.Position = 0;
    
                FileSavePicker savePicker = new FileSavePicker();
                savePicker.DefaultFileExtension = ".pdf";
                savePicker.SuggestedFileName = "Final";
                savePicker.FileTypeChoices.Add("PDF", new List<string>() { ".pdf" });
                StorageFile stFile = await savePicker.PickSaveFileAsync();
                myblock.Text = "Saving...";
                if (stFile != null)
                {
                    Windows.Storage.Streams.IRandomAccessStream fileStream = await stFile.OpenAsync(FileAccessMode.ReadWrite);
                    Stream st = fileStream.AsStreamForWrite();
                    st.SetLength(0);
    
                    long length = stream.Length;
    
                    while (i < length)
                    {
                        bufflist.Insert(i, Convert.ToByte(stream.ReadByte()));
                        st.WriteByte(bufflist.ElementAt(i));
                        i++;
                    }
    
                    await st.FlushAsync();
                    st.Dispose();
                    fileStream.Dispose();
                    myring.IsActive = false;
                    myblock.Visibility = Visibility.Collapsed;
                }
    Method 2 :
    stream.Position = 0;
    
                FileSavePicker savePicker = new FileSavePicker();
                savePicker.DefaultFileExtension = ".pdf";
                savePicker.SuggestedFileName = filename;
                savePicker.FileTypeChoices.Add("Adobe PDF Document", new List<string>() { ".pdf" });
                StorageFile stFile = await savePicker.PickSaveFileAsync();
                if (stFile != null)
                {
                    Windows.Storage.Streams.IRandomAccessStream fileStream = await stFile.OpenAsync(FileAccessMode.ReadWrite);
                    Stream st = fileStream.AsStreamForWrite();
                    st.SetLength(0);
                    st.Write((stream as MemoryStream).ToArray(), 0, (int)stream.Length);
                    st.Flush();
                    st.Dispose();
                    fileStream.Dispose();
                }

    Tuesday, June 23, 2015 6:48 AM

Answers

  • Hi kranthi88,

    Welcome to the Developing Universal Windows apps forum! 

    What type of app are you writing? Please read the sticky posts, especially the Guide to posting: subject line tags and Known Issues for Windows 10 SDK and Tools .

    Back to your question, theses two methods are correct, I think the second one should be simple, I will use it.

    As a suggestion, you need to make sure that the "stream" variable should not be null:

    private async void btnSave_Click(object sender, RoutedEventArgs e)
    {
                if (stream != null)
                {
                    stream.Position = 0;
                    FileSavePicker savePicker = new FileSavePicker();
                    savePicker.DefaultFileExtension = ".pdf";
                    savePicker.SuggestedFileName = "test";
                    savePicker.FileTypeChoices.Add("Adobe PDF Document", new List<string>() { ".pdf" });
                    StorageFile stFile = await savePicker.PickSaveFileAsync();
                    if (stFile != null)
                    {
                        Windows.Storage.Streams.IRandomAccessStream fileStream = await stFile.OpenAsync(FileAccessMode.ReadWrite);
                        Stream st = fileStream.AsStreamForWrite();
                        st.SetLength(0);
                        st.Write((stream as MemoryStream).ToArray(), 0, (int)stream.Length);
                        st.Flush();
                        st.Dispose();
                        fileStream.Dispose();
                    }
                }
    }


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, June 24, 2015 2:36 AM
  • Are u saying that method 2 is most efficient & fast or is it just a matter of preference ?

    Hi kranthi88,

    >>Are u saying that method 2 is most efficient & fast or is it just a matter of preference ?

    In my view, the second method is more efficient, there is no need to write Bytes by looping through byte list.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, June 25, 2015 12:28 PM

All replies

  • Hi kranthi88,

    Welcome to the Developing Universal Windows apps forum! 

    What type of app are you writing? Please read the sticky posts, especially the Guide to posting: subject line tags and Known Issues for Windows 10 SDK and Tools .

    Back to your question, theses two methods are correct, I think the second one should be simple, I will use it.

    As a suggestion, you need to make sure that the "stream" variable should not be null:

    private async void btnSave_Click(object sender, RoutedEventArgs e)
    {
                if (stream != null)
                {
                    stream.Position = 0;
                    FileSavePicker savePicker = new FileSavePicker();
                    savePicker.DefaultFileExtension = ".pdf";
                    savePicker.SuggestedFileName = "test";
                    savePicker.FileTypeChoices.Add("Adobe PDF Document", new List<string>() { ".pdf" });
                    StorageFile stFile = await savePicker.PickSaveFileAsync();
                    if (stFile != null)
                    {
                        Windows.Storage.Streams.IRandomAccessStream fileStream = await stFile.OpenAsync(FileAccessMode.ReadWrite);
                        Stream st = fileStream.AsStreamForWrite();
                        st.SetLength(0);
                        st.Write((stream as MemoryStream).ToArray(), 0, (int)stream.Length);
                        st.Flush();
                        st.Dispose();
                        fileStream.Dispose();
                    }
                }
    }


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, June 24, 2015 2:36 AM
  • Are u saying that method 2 is most efficient & fast or is it just a matter of preference ?
    Wednesday, June 24, 2015 4:40 AM
  • Are u saying that method 2 is most efficient & fast or is it just a matter of preference ?

    Hi kranthi88,

    >>Are u saying that method 2 is most efficient & fast or is it just a matter of preference ?

    In my view, the second method is more efficient, there is no need to write Bytes by looping through byte list.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, June 25, 2015 12:28 PM
  • There's an even faster method.

    https://msdn.microsoft.com/en-us/library/system.io.stream.copyto%28v=vs.110%29.aspx

    Thursday, June 25, 2015 1:34 PM
  • @Franklin Thanx for my concept clearance!!
    Friday, June 26, 2015 5:25 AM
  • @mcosmin are u sure that Stream.CopyTo method is faster, efficient & most reliable way to save a stream to a file coz my resultant stream comprises of hundreds of mega bytes, i don't want my app to throw out of memory exception.
    Friday, June 26, 2015 5:29 AM
  • Depends on the type of stream, of course!

    If your stream is so large, perhaps you should use method 2 you described. But there's always room for testing. Try it out and see if it works.

    Friday, June 26, 2015 6:37 AM