How do you delete a file in C#?
How do you delete a file in C#? Explain with example.
Ravi Vishwakarma is a dedicated Software Developer with a passion for crafting efficient and innovative solutions. With a keen eye for detail and years of experience, he excels in developing robust software systems that meet client needs. His expertise spans across multiple programming languages and technologies, making him a valuable asset in any software development project.
Anubhav Kumar
19-May-2025To delete a file in C#, use the
File.Delete()method from theSystem.IOnamespace.Syntax:
path: The full path of the file to delete.Example:
Notes:
UnauthorizedAccessException: If you don't have permission.IOException: If the file is in use by another process.ArgumentException/PathTooLongException: If the path is invalid.Optional: Safe delete with try-catch