Find absolute path in c# [duplicate] - c#

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

Related

File name from a string with file path C# [duplicate]

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

Extract part of a path in c# [duplicate]

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

How do I get the file name with it's extension? [duplicate]

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

Why is this invalid file path with // working? [duplicate]

This question already has answers here:
Using / or \\ for folder paths in C#
(5 answers)
Closed 7 years ago.
I have this function in a certain Class1:
public void function1(String path){
this.excel = new Application();
this.wbooks = excel.Workbooks;
this.wb = wbooks.Open(path);
String rootPath = wb.Path+"//..//..//";
String nPath = String.Format("{0}//Loads//{1}//{2}",rootPath,name1,name2);
String outputDir = String.Format("{0}//Input//{1}//{2}", rootPath, name1, name2);
String erroDir = String.Format("{0}//Erro//{1}//{2}", rootPath, name1, name2);
for(int i = 0; i < size; i++){
String[] array2 = File.ReadAllLines(String.Format("{0}//{1}_{2}.txt", nPath, name1, i.ToString()));
//code
Directory.CreateDirectory(erroDir);
File.WriteAllLines(String.Format("{0}//erro_{1}_{2}.txt", erroDir, name1, i.ToString()), array);
Directory.CreateDirectory(outputDir);
File.WriteAllLines(String.Format("{0}//output_{1}.txt", outputDir, name2), array);
}
}
This function from a class and is being called like this in the main function:
String path = "C:\\Users\\myUsername\\Desktop\\myFolder\\";
Class1 temp = new Class1();
temp.function1(path);
Why are the paths made in the function working?Shouldnt path be made "\\" instead of "//".
Why are the paths made in the function working?Shouldnt path be made "\" instead of "//".
Yes, they should. But Windows is 'smart'. It tries to determine what you actually meant to ask, in this case, if the file path starts with C: for example, it 'knows' it is a local file path, and thus it tries to interpret it as such.
Note that not all programs are that smart. Some .NET build tools for example (not C# itself) are known for some bugs with handling such file paths.

Return filename without extension from full path in C# [duplicate]

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("."));

Categories

Resources