Skip to content

Instantly share code, notes, and snippets.

@jmarolf
Created November 28, 2017 00:30
Show Gist options
  • Select an option

  • Save jmarolf/3d5e95a55a07f76e10c2e45c7dfbe1bd to your computer and use it in GitHub Desktop.

Select an option

Save jmarolf/3d5e95a55a07f76e10c2e45c7dfbe1bd to your computer and use it in GitHub Desktop.
public static void CopyFile(string from, string into) 
{
    using (var fromFile = File.Open(from, FileMode.Open, FileAccess.Read))
    using (var toFile = File.Open(into, FileMode.Create, FileAccess.Write))
    {
    // ... Perform actual file copy

    // Both FileStream objects are disposed at the end of the enclosing scope, 
    // in this case, the method declaration.
    }
}
public static void CopyFile(string from, string into) 
{
    using scoped var fromFile = File.Open(from, FileMode.Open, FileAccess.Read);
    using scoped var toFile = File.Open(into, FileMode.Create, FileAccess.Write);
    // ... Perform actual file copy

    // Both FileStream objects are disposed at the end of the enclosing scope, 
    // in this case, the method declaration.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment