none
Better Approach c# code - Memory stream RRS feed

  • Question

  • Hi, I am using below c# code which is working fine. But I will be receiving 5-10MB files which using this process have to load in memory and I think its a bit risky and throttling issue may arise. Can any of you experts please advise is below code is good approach ?

    2. Also, I am doing a File.copy.

      But if File Exist in the folder, I need to overwrite it. How to do this ?

    3. After loading in the memory, I need to delete the file one after another from Source folder. Where should I do this ? 

    public void LoadFiles ()
    {
    
    byte[] input = null;
                var files = Directory.EnumerateFiles(SourcePath, "*.pdf")
                        .OrderBy(f => f)
                        .Take(NumberOfFiles);
                string[] abc = files.ToArray();
    
    
                for (int i = 0; i < files.Count(); i++)
                {
                    if (File.Exists(abc[i]))
                    {
                        //Archieving the files
                       // File.Copy(abc[i], String.Concat(ArchievePath, Path.GetFileName(abc[i])));
                        File.WriteAllText(abc[i], String.Concat(ArchievePath, Path.GetFileName(abc[i])));
    
    
                        Stream fs = File.OpenRead(abc[i]);
                        using (MemoryStream memoryStream = new MemoryStream())
                        {
                            fs.CopyTo(memoryStream);
                            input = memoryStream.ToArray();
                        }
    
                        CreateOutgoingMessage(pContext, input, pInMsg, Path.GetFileName(abc[i]));
    
                        fs.Close();
                       // File.Delete(abc[i]);
                    }
    				}


    Reason101

    Thursday, April 2, 2020 1:54 AM

All replies

  • Hi Reason101,
    1.>>But I will be receiving 5-10MB files 
    Based on your code, it will not create new files. Could you explain this in detail?
    2.File.copy:

    File.Copy(Path.Combine(sourceDir, fName), Path.Combine(backupDir, fName), true);

    You can use the Path.Combine method to safely append the file name to the path and it will overwrite if the destination file already exists.
    3.Delete the file:
    After copying the files then delete them from the source folder.

       foreach (string f in abc)
        {
            File.Delete(f);
        }

    Best Regards,
    Daniel Zhang


    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.

    Thursday, April 2, 2020 7:56 AM
  • Hi Reason101,
    Has your problem been solved? If it is resolved, we suggest that you mark it as the answer. So it can help other people who have the same problem find a solution quickly.
    Best Regards,
    Daniel Zhang


    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.

    Thursday, April 23, 2020 9:07 AM