Skip to content

Instantly share code, notes, and snippets.

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

  • Save jmarolf/5e40baaacfc5a6e7cd3999b75f248abd to your computer and use it in GitHub Desktop.

Select an option

Save jmarolf/5e40baaacfc5a6e7cd3999b75f248abd 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.
    }
}

``C# 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