Net-informations.com
SiteMap  | About    

How to C# Path Class

C# provides effective ways of dealing with filenames and paths from its namespace System.IO . The Path Class performs operations on String instances that contain file or directory path information. The members of the Path class enable you to quickly and easily perform common operations such as returns filenae , extensios etc.

The following are the some important operations in C# Path Class:

GetDirectoryName - Returns the directory information for the specified path string.

GetExtension - Returns the extension of the specified path string.

GetFileName - Returns the file name and extension of the specified path string.

GetFileNameWithoutExtension - Returns the file name of the specified path string without the extension.

GetFullPath - Returns the absolute path for the specified path string.

Get Current Application Path

C# Class application in System.Windows.Forms namespace has static property called ExecutablePath . To get the current application path we can use GetDirectoryName of Path class with ExecutablePath as parameter.

  applicationPath = Path.GetDirectoryName(Application.ExecutablePath)

It will return the current directory path of the .exe file .




using System;
using System.Windows.Forms;
using System.IO;

namespace WindowsFormsApplication1
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
		}

		private void button1_Click(object sender, EventArgs e)
		{
			string tmpPath = "c:\\windows\\inf\\wvmic.inf";

			string fileExtension = Path.GetExtension(tmpPath);
			string filename = Path.GetFileName(tmpPath);
			string filenameWithoutExtension = Path.GetFileNameWithoutExtension(tmpPath);
			string rootPath = Path.GetPathRoot(tmpPath);
			string directory = Path.GetDirectoryName(tmpPath);
			string fullPath = Path.GetFullPath(tmpPath);

			MessageBox.Show(directory);

		}
	}
}






net-informations.com (C) 2017    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.