Is FileInfo.Exists/Copy faster than File? [duplicate] - c#

This question already has answers here:
What is the difference between File and FileInfo in C#?
(10 answers)
Closed 9 years ago.
As the title says is which one of the scenarios below is faster?
// Using FileInfo
FileInfo file = new FileInfo(#"C:\Test.txt");
if (file.Exists)
file.CopyTo(#"C:\TestCopy.txt");
// Using File
if (File.Exists(#"C:\Test.txt"))
File.Copy(#"C:\Test.txt", #"C:\TestCopy.txt");
I know the FileInfo is easier for the eye to read, but is one method faster than the other?

Difference is that FileInfo cache information: file existing check is executed once.
Then, if you check Exists property and THEN create file, new call to Exists property always will return false.

Related

How to use Filestream with embedded resource file? [duplicate]

This question already has answers here:
How to read embedded resource text file
(23 answers)
C# Get full path of embedded ressource? [duplicate]
(2 answers)
Closed 3 years ago.
I have a code that opens file using filestream as below:-
using (var stream = new FileStream("abc.txt", FileMode.Open, FileAccess.ReadWrite))
I do not want to have it in any any directory rather I want it in embedded resource. I just copied the file in solution explorer and changed build action to embedded resource, now what should I do next to get it worked and achieve similar code behavior as above!
Thanks

How may i use File.exists in Datatable.select expression [duplicate]

This question already has answers here:
Check if value exists in dataTable?
(5 answers)
Closed 6 years ago.
I have a datatable which contain a column Path of file. Now i want to filter file Path is exist or not.
DataTable.Select(File.Exists(ColumnsName))
Would you please help me how may i filter.
You can filter datatable by file path column by checking existence using File.Exists
var result = dataTable.AsEnumerable().Where(r=>File.Exists(r.Field<string>("Path"));
DataSets are a pretty old concept in .NET so to use LINQ you need a bit of extra syntax:
dataTable.Rows.Cast<DataRow>().Select(row => File.Exists(row.Field<String>(columnName)))
This will return an IEnumerable<Boolean> that determines if the files exist.

In File.Exists() how to know if the file is the reall file or a shortcut? [duplicate]

This question already has answers here:
Check if a file is real or a symbolic link
(9 answers)
How do i get the path name from a file shortcut ? Getting exception [duplicate]
(1 answer)
Closed 7 years ago.
I have a file in the folder like "c:\a\abc.mdb"
Sometimes someone could move it to somewhere else and leave a shortcut like "c:\a\abc.mdb.lnk".
In that case when I call IO.File.Exists(#"c:\a\abc.mdb") it still return true. In that case how can I verify if the real file exists, or whether the situation is:
abc.mdb exists only
abc.mdb.lnk exists only
or both of them exists?

How to sort DirectoryInfo.GetFiles() [duplicate]

This question already has answers here:
Sorting Directory.GetFiles()
(13 answers)
Closed 8 years ago.
I am creating the image of the PowerPoint file programmatically. And after saving the Images to the local drive I am getting the files using DirectoryInfo.GetFiles().
I am saving the image files with the sequence numbers.
My Files:
My issue is when I get the files, are not in the sequence I need them in.
The files sequence which I am getting in the FileInfo[] is :
Can any one help me to solve this issue?
The function doesn't make any guarantees about order but you can achieve the desired result with a simple LINQ query;
FileInfo[] sortedFiles = DirectoryInfo.GetFiles().OrderByDescending(x => x.Name).ToArray();
Try this
foreach (FileInfo fi in directory.GetFiles().OrderBy(fi=>fi.FileName))
{
}

How do I read the contents of an embedded text file? [duplicate]

This question already has answers here:
How to read embedded resource text file
(23 answers)
Closed 9 years ago.
I have a text file data.txt that's embedded in my solution (as described in this SO question).
How do I read the contents of this file as a string? I'm imagining something like this:
string data = Resources["data.txt"];
but that's not the way to do it.
If you add the file as a resource you should be able to access it like this:
Properties.Resources.data
Or alternatively if you set the Copy to Output Directory property to Copy always/Copy if newer, you can do something like:
using (FileStream fs = System.IO.File.Open("Resources/data.txt", FileMode.Open))
{
// do amazing stuff here ...
}

Categories