This question already has answers here:
How can I compare (directory) paths in C#?
(14 answers)
Closed 8 years ago.
I need to check whether two relative file paths are equal or not.
String path1 = "a/b/c/file.txt";
String path2 = "/A/B/../B/C/file.txt";
Actually those two paths point to the same file. But how can I check this?
PS: Doing this for absolute paths works fine, with the approach described here.
You can use Path.GetFullPath (MSDN) to compare the absolute paths of your files.
Related
This question already has answers here:
How to use Directory.GetFiles to get only gif types (or any other type)?
(2 answers)
Closed 6 years ago.
is it possible to get the names of .resx files? In runtime. I need to capture those names to get the available languages and load them into a combobox.
I have a folder
ProjectName
Multilanguage
Language.resx
Language.en-US.resx
Language.fr-FR.resx
I need in an array the names Language.resx, Language.en-US.resx and Language.fr-FR.resx
Thx!
You can use the GetFiles method on the Directory class to get all files recursively. It even supports a pattern:
string[] result = System.IO.Directory.GetFiles(#"C:\source\project",
"*.resx", System.IO.SearchOption.AllDirectories);
This question already has answers here:
C# - How to extract the file name and extension from a path?
(4 answers)
Closed 8 years ago.
Is it possible to separate the name of the file to it's file type/ file extension.
For example I have this file named sample.text. I want to get separate sample and .txt using c#.
Can anyone help me. Thanks.
You can use Path.GetExtension:
var extension =
Path.GetExtension("C:\\sample.txt"); // returns txt
..and Path.GetFileNameWithoutExtension:
var fileNameWithoutExtension =
Path.GetFileNameWithoutExtension("C:\\sample.txt"); // returns sample
This question already has answers here:
File.Exists returning true for a file that doesn't exist
(2 answers)
Closed 9 years ago.
I am trying to detect the existence of a file, I am using the System.IO.File.Exists function from C#, so, if I do File.Exists("file.txt"); it returns TRUE, although the file does NOT exist!!
What is happening here?
If the method is returning true, then the file does exist.
However, you are probably not looking at the same location as the File.Exists command.
From MSDN
The path parameter is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory.
As you are not specifying the full path to the file, the current working directory is being used to determine if the file exists; and it likely does exist there.
When you use just the fileName, it will find in the same local where it is executing. You have to pass the full path, for sample:
if (File.Exists("C:\\temp\\file.txt"))
{
// exists, use it
}
else
{
// does not exists
}
This question already has answers here:
How can one get an absolute or normalized file path in .NET?
(4 answers)
Closed 9 years ago.
How to get canonical file name by non-canonical one.
E.g. I want to call function which converts "C:\Program files\..\Windows\aaa.txt" to "C:\Windows\aaa.txt"
I am looking for something like Java File.getCanonicalPath()
You can use the Path.GetFullPath method for this.
Example:
Console.WriteLine(Path.GetFullPath(#"C:\Program files\..\Windows\aaa.txt"));
Output:
C:\Windows\aaa.txt
System.IO.Path.GetFullPath("C:/Program files/../Windows/aaa.txt")
will return
"C:\\Windows\\aaa.txt"
Here is my suggestion:
string path = Path.GetFullPath(#"C:\Program files\..\Windows\aaa.txt");
This question already exists:
Closed 12 years ago.
Possible Duplicate:
String vs string in C#
What is the big difference between these datatypes? and were should I use it?
Short answer, there is no difference. They are just alias of each other, see
http://msdn.microsoft.com/en-us/library/ya5y69ds(VS.80).aspx for a complete list.
Have a look at: String vs string in C#