Getsuyodev, Programming is mainly used when constructing an application. Programming requires knowledge of application domains, algorithms, and programming language expertise. Programming can be developed in different logic based on developer knowledge.

How to C-Sharp 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 everyday operations such as returns filename, extensions etc.

The following are some crucial 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 a static property called ExecutablePath . To get the current application path, we can use GetDirectoryName of Path class with ExecutablePath as a parameter.

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);

		}
	}
}

Comments are closed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More