Thanks to visit codestin.com
Credit goes to github.com

Skip to content

IOException: Sharing violation on path ... #16032

@andreysavihin

Description

@andreysavihin

Hello!
I got exception in my code when i try to copy files using FileStream with FileShare parameter.

Steps to Reproduce

  1. create Program.cs file
using System;
using System.IO;

namespace FileCopy
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                File.WriteAllText("source.txt", "text");
                var fileStream = new FileStream("source.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite, 2048, FileOptions.DeleteOnClose);
                File.Copy(fileStream.Name, "target.txt", true);
            }
            catch (Exception e)
            {
                File.WriteAllText("error.txt", e.Message + Environment.NewLine + e.StackTrace);
            }
        }
    }
}
  1. run build command

mcs -out:Test.exe Program.cs

  1. execute

mono Test.exe

Current Behavior

got file error.txt with error & stack trace
IOException: Sharing violation on path .../source.txt

Expected Behavior

got file target.txt with "text" content.
file source.txt will be removed, because used FileOptions.DeleteOnClose parameter

[ ] macOS
[x] Linux
[ ] Windows

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions