Concatenating Strings to Build File Paths

Here is an easy one – more so for my reference but it may help others. Let’s suppose you are concatenating strings to build a file path. Perhaps you have an application that automatically generates invoices in docx format and you need to save these files in a specific folder. Each file needs to be named based on the full name of a customer. There is no right or wrong way to do it, but we might as well utilize .NET methods if they make this process simpler.

Example 1: Using string.Format

string filePath = string.Format(@”C:\myfolder\mysubfolder\{0}.txt”, c.FullName);

Example 2: Using Path.Combine

string filePath = Path.Combine(@”C:\myfolder\mysubfolder\”, c.FullName);

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s