Program that uses Path.Combine [C#]

Posted by senthil | 8:18:00 PM | , | 0 comments »

Path.Combine is a useful method, but there are edge cases it cannot solve. It can't figure out what you want if what it receives is confusing. But different inputs can yield the same result path. Next: Here's a screenshot where we combine the folder "Content\\" with the file name "file.txt".
using System;

class Program
{
    static void Main()
    {
 //
 // Combine two path parts.
 //
 string path1 = System.IO.Path.Combine("Content", "file.txt");
 Console.WriteLine(path1);

 //
 // Same as above but with a trailing separator.
 //
 string path2 = System.IO.Path.Combine("Content\\", "file.txt");
 Console.WriteLine(path2);
    }
}
Output

Content\file.txt
Content\file.txt
Read more ...

Program that uses GetExtension [C#]

Posted by senthil | 8:14:00 PM | , | 0 comments »

The Path type includes also support for extensions. We can get an extension, with GetExtension, or even change an extension with ChangeExtension. The method names are obvious and easy-to-remember. GetExtension handles extensions of four letters. It also handles the case where a file name has more than one period in it. This next program briefly tests GetExtension. You can find further details and benchmarks.
using System;
using System.IO;

class Program
{
    static void Main()
    {
 // ... Path values.
 string value1 = @"C:\perls\word.txt";
 string value2 = @"C:\file.excel.dots.xlsx";

 // ... Get extensions.
 string ext1 = Path.GetExtension(value1);
 string ext2 = Path.GetExtension(value2);
 Console.WriteLine(ext1);
 Console.WriteLine(ext2);
    }
}
Output

.txt
.xlsx
Read more ...

Program that uses verbatim string [C#]

Posted by senthil | 8:08:00 PM | , | 0 comments »

Extensions GetFileName­WithoutExtension will return the entire file name if there's no extension on the file.Path.GetDirectoryName returns the entire string except the file name and the slash before it.

Syntax


When specifying paths in C# programs, we must use two backslashes "\\" unless we use the verbatim string syntax. A verbatim string uses the prefix character "@". Only one backslash is needed in this literal syntax.
using System;
using System.IO;

class Program
{
    static void Main()
    {
 // ... Verbatim string syntax.
 string value = @"C:\directory\word.txt";
 Console.WriteLine(Path.GetFileName(value));
    }
}
OutPut
word.txt
Read more ...

Program that tests Path class [C#]

Posted by senthil | 7:56:00 PM | , | 0 comments »

The extension of the file, the actual filename, the filename without extension, and path root. The path root will always be "C:\\", with the trailing separator, even when the file is nested in many folders. GetFileName. You can get the filename alone by calling the Path.GetFileName method. This will return the filename at the end of the path, along with the extension, such as .doc or .exe.

extension —Path.GetFileNameWithoutExtension.

It is useful to see the results of the Path methods on various inputs. Sometimes the methods handle invalid characters as you might expect. Sometimes they do not. This program calls three Path methods on an array of possible inputs.
using System;
using System.IO;

class Program
{
    static void Main()
    {
 string[] pages = new string[]
 {
     "cat.aspx",
     "really-long-page.aspx",
     "test.aspx",
     "invalid-page",
     "something-else.aspx",
     "Content/Rat.aspx",
     "http://dotnetperls.com/Cat/Mouse.aspx",
     "C:\\Windows\\File.txt",
     "C:\\Word-2007.docx"
 };
 foreach (string page in pages)
 {
     string name = Path.GetFileName(page);
     string nameKey = Path.GetFileNameWithoutExtension(page);
     string directory = Path.GetDirectoryName(page);
     //
     // Display the Path strings we extracted.
     //
     Console.WriteLine("{0}, {1}, {2}, {3}",
  page, name, nameKey, directory);
 }
    }
}
OutPut

Input:                       cat.aspx
GetFileName:                 cat.aspx
GetFileNameWithoutExtension: cat
GetDirectoryName:            -

Input:                       really-long-page.aspx
GetFileName:                 really-long-page.aspx
GetFileNameWithoutExtension: really-long-page
GetDirectoryName:            -

Input:                       test.aspx
GetFileName:                 test.aspx
GetFileNameWithoutExtension: test
GetDirectoryName:            -

Input:                       invalid-page
GetFileName:                 invalid-page
GetFileNameWithoutExtension: invalid-page
GetDirectoryName:            -

Input:                       Content/Rat.aspx
GetFileName:                 Rat.aspx
GetFileNameWithoutExtension: Rat
GetDirectoryName:            Content

Input:                       http://dotnetperls.com/Cat/Mouse.aspx
GetFileName:                 Mouse.aspx
GetFileNameWithoutExtension: Mouse
GetDirectoryName:            http:\dotnetperls.com\Cat

Input:                       C:\Windows\File.txt
GetFileName:                 File.txt
GetFileNameWithoutExtension: File
GetDirectoryName:            C:\Windows

Input:                       C:\Word-2007.docx
GetFileName:                 Word-2007.docx
GetFileNameWithoutExtension: Word-2007
GetDirectoryName:            C:\
Read more ...

Program that uses Path methods [C#]

Posted by senthil | 7:40:00 PM | , | 0 comments »

Path handles file path processing.Its a major thing the Path type in the System.IO namespace. Its very critical when using directly with paths.

Examples

If you need to extract filename path in your program. You can access it by adding "using System.IO" at the top of your class.

Console


using System;
using System.IO;

class Program
{
    static void Main()
    {
 string path = "C:\\stagelist.txt";

 string extension = Path.GetExtension(path);
 string filename = Path.GetFileName(path);
 string filenameNoExtension = Path.GetFileNameWithoutExtension(path);
 string root = Path.GetPathRoot(path);

 Console.WriteLine("{0}\n{1}\n{2}\n{3}",
     extension,
     filename,
     filenameNoExtension,
     root);
    }
}
Output

.txt
stagelist.txt
stagelist
C:\
Read more ...

Related Posts Plugin for WordPress, Blogger...