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

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.

Related

Why does Path.Combine ignore the first part of a ftp server path? [duplicate]

This question already has answers here:
Why does Path.Combine not properly concatenate filenames that start with Path.DirectorySeparatorChar?
(16 answers)
Closed 5 years ago.
I have two parts of a ftp server path. Let's name the /one and /two.
The full path I want to achieve is /one/two.
When I use System.IO.Path.Combine("/one", "/two"); the output is "/two".
Why is that so? I expected it to be "/one/two" as you would normally with windows paths.
Is this by design, wrong usage by me or a bug in the .net class (unlikely)?
(How) can I use System.IO.Path functionality to produce "/one/two"?
If path2 contains an absolute path, this method returns path2.

cut last slash and item for path in c# [duplicate]

This question already has answers here:
How do I find the parent directory of a path?
(4 answers)
Closed 6 years ago.
I would like to cut last slash and corresponding file or folder given path in string variable (called pathVar)
For example, if pathVar = "c:\temp\a\b\c"
I would like to assign to newPathVar = "c:\temp\a\b"
What is the easier and best way in c# to do that?
Thanks!
You can use Path.GetDirectoryName for that.

Find files not matching a pattern [duplicate]

This question already has answers here:
Using Directory.GetFiles with a regex in C#?
(4 answers)
Exclude certain file extensions when getting files from a directory
(9 answers)
Closed 9 years ago.
For finding all .txt files, we can use this:
Directory.GetFiles(#"c:\","*.txt")
Is there any way to find all files not matching a pattern (for ex: all files not having extension .txt).
You can try LINQ:
var files = Directory.EnumerateFiles("C:\\").Where(x => !x.EndsWith(".txt")).ToList();
No builtin way as search pattern. But you could use Linq:
var files = Directory.EnumerateFiles(dir)
.Where(fn => !Path.GetExtension(fn).Equals(".txt", StringComparison.OrdinalIgnoreCase))
.ToArray();
Note that i've used EnumerateFiles instead of GetFiles. The latter loads al files into memory before you can start processing, with EnumerateFiles you can start enumerating and filtering the collection of names before the whole collection is returned.
use linq
var files = Directory.GetFiles(dir)
.Where(file=> !file.EndsWith(".txt").ToList();

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

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.

How can I check the existence of database [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to check for the existence of a DB?
How can I check the existance of database and if it exist how can I delete it?
Check out this page:
http://akrabat.com/winphp-challenge/retriving-a-list-of-database-from-sql-server/
It tells you how to retrieve a list of databases on an sql-server. After that, I think a
DROP DATABASE ...
should do the trick.

Categories