Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
How to get full path of a file in C#
WhatsApp
Mahesh Chand
6y
285.4k
0
5
100
Article
Get Full Path of a File
The FullName property returns just the full path of a file including the file name. The following code snippet returns the full path of a file.
string
fullFileName = fi.FullName;
Console.WriteLine(
"File Name: {0}"
, fullFileName);
Sample
Here is a complete sample.
// Full file name
string
fileName = @
"/Users/praveen/Desktop/images\November.pdf"
;
FileInfo fi =
new
FileInfo(fileName);
// Create a new file
using
(FileStream fs = fi.Create())
{
Byte[] txt =
new
UTF8Encoding(
true
).GetBytes(
"New file."
);
fs.Write(txt, 0, txt.Length);
Byte[] author =
new
UTF8Encoding(
true
).GetBytes(
"Author Mahesh Chand"
);
fs.Write(author, 0, author.Length);
}
// Get File Name
string
justFileName = fi.Name;
Console.WriteLine(
"File Name: {0}"
, justFileName);
// Get file name with full path
string
fullFileName = fi.FullName;
Console.WriteLine(
"File Name: {0}"
, fullFileName);
// Get file extension
string
extn = fi.Extension;
Console.WriteLine(
"File Extension: {0}"
, extn);
// Get directory name
string
directoryName = fi.DirectoryName;
Console.WriteLine(
"Directory Name: {0}"
, directoryName);
// File Exists ?
bool
exists = fi.Exists;
Console.WriteLine(
"File Exists: {0}"
, exists);
if
(fi.Exists)
{
// Get file size
long
size = fi.Length;
Console.WriteLine(
"File Size in Bytes: {0}"
, size);
// File ReadOnly ?
bool
IsReadOnly = fi.IsReadOnly;
Console.WriteLine(
"Is ReadOnly: {0}"
, IsReadOnly);
// Creation, last access, and last write time
DateTime creationTime = fi.CreationTime;
Console.WriteLine(
"Creation time: {0}"
, creationTime);
DateTime accessTime = fi.LastAccessTime;
Console.WriteLine(
"Last access time: {0}"
, accessTime);
DateTime updatedTime = fi.LastWriteTime;
Console.WriteLine(
"Last write time: {0}"
, updatedTime);
}
Output
Next>>
FileInfo in C#
.NET file path
C# file path
directory of a file
FileInfo.FullName
full path of a file
Up Next
Ebook Download
View all
Printing in C# Made Easy
Read by 22.4k people
Download Now!
Learn
View all
Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.
Membership not found