This question already has answers here:
Given a filesystem path, is there a shorter way to extract the filename without its extension?
(10 answers)
Closed 7 years ago.
I have tried this but this only gets the file name not the extension. How do I get file name with extension?
string[] FileList = (string[])e.Data.GetData(DataFormats.FileDrop, false);
string filename = Path.GetFileNameWithoutExtension(FileList[0]);
Path.GetFileName()
string[] FileList = (string[])e.Data.GetData(DataFormats.FileDrop, false);
string filename = Path.GetFileName(FileList[0]);
Check the documentation
Related
This question already has answers here:
Given a filesystem path, is there a shorter way to extract the filename without its extension?
(10 answers)
Closed 10 months ago.
Suppose I have an array of strings with full file names and paths. For example
string[] filesArray = Directory.GetFiles(#"C:\dir", "*", SearchOption.AllDirectories);
Now let's say we have the following data in the array:
filesArray[0] = "C:\dir\file1.txt"
filesArray[1] = "C:\dir\subdir1\file2.txt"
filesArrat[2] = "C:\dir\subdir1\subdir2\file3.txt"
... etc.
Now I want a new array, that will store only the files' names, something like this:
nameArray[0] = "file1.txt"
nameArray[1] = "file2.txt"
nameArray[2] = "file3.txt"
What is the best way to do it, using string array only, without storing the full FileInfo class objects?
Using LINQ it's pretty simple
string[] nameArray = filesArray.Select(p => Path.GetFileName(p)).ToArray()
This question already has answers here:
Relative path to absolute path in C#?
(8 answers)
Closed 2 years ago.
I'm trying to get the full path to a file.
I have the "end" of the path like:
\some_folder\myTextFile.txt
and the "beginning" like:
c:\Users\me\...
how do I find the absolute path to the textfile?
using System.IO
(...)
string relativePath = "\some_folder\myTextFile.txt";
string absolutePath = Path.GetFullPath(relativePath);
should work
string [] fileEntries = Directory.GetFiles(targetDirectory);
foreach(string a in fileEntries)
{
if(a.Contains("\some_folder\myTextFile.tx"))
{
return a
}
else
{
continue;
}
}
This question already has answers here:
How do I get a relative path from one path to another in C# [duplicate]
(2 answers)
Closed 2 years ago.
Suppose I have the directory path:
D:\aa\bb
and inside of it can be more file or folders with sub folders
for example
D:\aa\bb\test.txt
D:\aa\bb\cc\test.txt
D:\aa\bb\cc\dd\test.txt
is there a clean way to extract the right part of the path?
I need something like:
string ExtractRightPart(string fullPath)
{
return ...
}
Examples:
For input
D:\aa\bb\cc\dd\test.txt
the function should return
cc\dd\test.txt
And for input
D:\aa\bb\test.txt
the function should return
test.txt
public string ExtractRightPart(string fullPath, string leftPath)
{
return fullPath.Substring(leftPath.Length);
}
ExtractRightPart(#"D:\aa\bb\cc\dd\test.txt", #"D:\aa\bb\");
This question already has answers here:
How to recursively list all the files in a directory in C#?
(23 answers)
Closed 9 years ago.
This is the code:
private void SearchForDoc()
{
t = Environment.GetEnvironmentVariable("UserProfile")+"\\documents";
string[] s = Directory.GetFiles(t, "*.txt",);
This will search for txt files in a directory but i want to find also all the text files in its subdirectories.
There should be SearchOption searchOption after the "*.txt",
Use this overload: Directory.GetFiles Method (String, String, SearchOption), where SearchOption is SearchOption.AllDirectories.
string[] s = Directory.GetFiles(t, "*.txt", SearchOption.AllDirectories);
string[] s = Directory.GetFiles(t,"*.txt", SearchOption.AllDirectories);
This question already has answers here:
Getting file names without extensions
(14 answers)
Closed 7 years ago.
I'm looking for a way to return fileName from full path but without extension.
private static string ReturnFileNameWithoutExtension(string varFullPathToFile) {
string fileName = new FileInfo(varFullPathToFile).Name;
string extension = new FileInfo(varFullPathToFile).Extension;
return fileName.Replace(extension, "");
}
Is there more bullet proof solution then replacing extension with empty string?
return Path.GetFileNameWithoutExtension (fullpath);
i'm using System.IO.Path.GetFileNameWithoutExtension(filename);
You're looking for Path.GetFileNameWithoutExtension
One More solution
string fileName = new FileInfo(varFullPathToFile).Name;
fileName=fileName.Substring(0, fileName.LastIndexOf("."));