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\");
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 can I check if a string exists in another string
(10 answers)
Closed 5 years ago.
I'm working on a Existing Class file(.cs) which fetches a string with some data in it.
I need to check if the string contains a word. String has no blank spaces in it.
The string-
"<t>StartTxn</t><l>0</l><s>0</s><u>1</u><r>0</r><g>1</g><t>ReleaseUserAuthPending</t>"
I need to check if the string contains 'ReleaseUserAuthPending' in it.
You can try this:
var strValue = "<t>StartTxn</t><l>0</l><s>0</s><u>1</u><r>0</r><g>1</g><t>ReleaseUserAuthPending</t>";
if (strValue.Contains("ReleaseUserAuthPending"))
{
//Do stuff
}
Refer About String - Contains function
For your information: Contains function is case-sensitive. If you want to make this Contains function as case-insensitive. Do the following step from this link.
bool containsString = mystring.Contains("ReleaseUserAuthPending");
Try
String yourString = "<t>StartTxn</t><l>0</l><s>0</s><u>1</u><r>0</r><g>1</g><t>ReleaseUserAuthPending</t>";
if(yourString.Contains("ReleaseUserAuthPending")){
//contains ReleaseUserAuthPending
}else{
//does not contain ReleaseUserAuthPending
}
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
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("."));