How to touch a file in C?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
To "touch" a file in C#, you usually want one of two behaviors: create the file if it does not exist, or update its modification timestamp if it already exists. .NET does not have a single built-in method named Touch, but the behavior is easy to implement with File.Create and File.SetLastWriteTimeUtc.
What touch Usually Means
On Unix systems, the touch command:
- creates an empty file if it does not exist
- updates the file timestamp if it does exist
A C# version should usually preserve that same behavior.
A Simple C# Touch Method
The basic implementation looks like this.
If the file already exists, this updates its last-write timestamp. If the file does not exist, it creates an empty file and closes the handle immediately.
Close the File Handle Immediately
When you create the file, disposing the stream right away is important.
Without the using block, the file can remain open longer than intended, which may cause later operations such as writing or deleting to fail.
Use UTC Timestamps for Predictable Behavior
If your application runs across time zones or different machines, SetLastWriteTimeUtc is usually safer than SetLastWriteTime.
UTC-based timestamps avoid local-time ambiguity and are easier to reason about in logs, sync tools, and build systems.
Create Parent Directories if Needed
A touch operation will fail if the parent directory does not exist. If the path may point into a missing directory tree, create that first.
This makes the method more practical in real applications where output paths are generated dynamically.
Decide Whether to Update Access Time Too
Classic touch behavior can vary depending on platform and flags. In C#, the main question is whether you want to update only the write time or also the access time.
For many application scenarios, last-write time is the meaningful signal. If your use case specifically depends on access time, you can update it with File.SetLastAccessTimeUtc as well.
Be Clear About Intent
Sometimes developers say "touch a file" when they really mean one of these narrower operations:
- ensure the file exists
- truncate the file
- append a line to the file
- update a timestamp only
Those are different behaviors. A true touch should not erase content and usually should not add content either.
That distinction matters in build scripts and cache invalidation code. A timestamp-only update can trigger downstream tooling without rewriting file contents, while an accidental truncate or append changes the file semantically and may introduce bugs that are much harder to diagnose.
Common Pitfalls
- Creating the file but forgetting to dispose the returned stream.
- Updating local time instead of UTC when cross-machine consistency matters.
- Assuming
File.Createwill succeed even if the parent directory does not exist. - Using a touch helper when the real requirement is append, truncate, or overwrite.
- Thinking a touch operation should modify file contents rather than metadata.
Summary
- In C#, touching a file means creating it if missing or updating its timestamp if present.
- Use
File.Exists,File.Create, andFile.SetLastWriteTimeUtcfor the core behavior. - Dispose the created file handle immediately.
- Create parent directories first if the path may not exist.
- Keep the behavior focused on existence and timestamps, not content changes.
Related reading
- How to truncate milliseconds off of a .NET DateTime
- How to turn off or handle camelCasing in JSON response ASP.NET Core?
- How to turn off the logging done by the ASP.NET core framework
- How to upload a file to amazon S3 super easy using c
- How to upload a file to amazon S3 super easy using c
- How to use a App.config file in WPF applications?
- How to use async with Visual Studio 2010 and .NET 4.0?
- How to use async/await on void methods in ASP.NET Core?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.