score:0
note: i haven't tested it, but in theory it should work
first, define a predicate to determine read access
bool canread(fileinfo file)
{
try {
file.getaccesscontrol();
//read and write access;
return true;
}
catch (unauthorizedaccessexception uae)
{
if (uae.message.contains("read-only"))
{
//read-only access
return true;
}
return false;
}
}
then, it should be a simple case of using a where clause in a linq query
from file in directoryinfo.getfiles("\\server\share\folder\")
where haveaccess(f) == true
select f;
score:0
tested and working, but will return false if the file is in use
void main()
{
var directoryinfo = new directoryinfo(@"c:\");
var currentuser = windowsidentity.getcurrent();
var files = directoryinfo.getfiles(".").where(f => canread(currentuser, f.fullname));
}
private bool canread(windowsidentity user, string filepath)
{
if(!file.exists(filepath))
return false;
try
{
var filesecurity = file.getaccesscontrol(filepath, accesscontrolsections.access);
foreach(filesystemaccessrule fsrule in filesecurity.getaccessrules(true, true, typeof(system.security.principal.securityidentifier)))
{
foreach(var usrgroup in user.groups)
{
if(fsrule.identityreference.value == usrgroup.value)
return true;
}
}
} catch (invalidoperationexception) {
//file is in use
return false;
}
return false;
}
score:1
just try this out .should work .haven't tested though
var fw = from f in new directoryinfo("c:\\users\\user\\downloads\\").getfiles()
where securitymanager.isgranted(new fileiopermission
(fileiopermissionaccess.write, f.fullname))
select f;
edit if it is just read only files then try this
var fe = from f in new directoryinfo("c:\\users\\ashley\\downloads\\").getfiles()
where f.isreadonly==true
select f
score:1
you run the risk of a race condition if you check for read permission prior to opening the file.
if you're attempting to read all of the files you have access to in a folder, better to just try opening each one and catch the unauthorizedaccessexception
.
see:
Source: stackoverflow.com
Related Query
- C# Linq for files user has read access to
- source code for LINQ 101 samples
- Read groupname for specific user in asp net linq
- How can I get LINQ to return the object which has the max value for a given property?
- Using LINQ for an object that only has GetEnumerator()
- Suggestions for designing complex LINQ code
- How to reuse a linq expression for 'Where' when using multiple source tables
- Best practices re: LINQ To SQL for data access
- LINQ Source Code Available
- Internal access for entities in Entity Framework makes simple linq where query crash
- linq - how do you do a query for items in one query source that are not in another one?
- Why do linq search has a huge difference in efficiency when I use string and array especially for large data?
- Determine the source DataContext for a Linq to Sql query
- dynamic property access for generated LINQ Classes
- creating Linq to sqlite dbml from DbLinq source code
- Stubbing Code for Test With Linq Expressions and Lambdas
- read icollection data using LINQ in C# code
- How to call user defined function in LINQ in my visual C# web service for my android application?
- Dynamic "Not" by parameter in LINQ (Or any other code for that matter)
- Understanding the various options for runtime code generation in C# (Roslyn, CodeDom, Linq Expressions, ...?)
- Using LINQ query result for data source for GridControl c#
- Linq ThenBy() throwing "The best overloaded method match ThenBy() for has some invalid arguments" exception
- Optimize LINQ to SQL statement that has foreign key access
- User defined filter for linq
- How can I refactor this code for LINQ filtering?
- How to get User Role for each user in Data access layer in Identity (MVC5)?
- Code Rewite for tuple and if else statements by using LINQ
- C# code for equivalent LINQ query
- Solution For Updating LINQ to SQL Files After Database Schema Change
- Linq dynamic queries for user search screens
More Query from same tag
- How do I solve server side pagination issue in JQGrid using mvc4
- Part of LINQ query to seperate method (EF Core)
- Anonymous cast type exception
- Grouping and multiple orderings using Linq to Entities
- Select from IEnumerable with Distinct/GroupBy and sorting — possible?
- Remove a value from comma separated string in C#
- Linq, how to specify timestamp condition?
- Why is my mocked list not removing a certain element?
- Linq Expression tree compiling non-trivial object constants and somehow referring to them
- LinQ getting distinct fields with average number error
- Linq: Order of execution chain query
- LINQ getting distinct records based on an item value
- LINQPad - can't understand the query generated
- LINQ - Where Conditions - "if-else" Cases
- how to do this with linq?
- Convert an IOrderedEnumerable<KeyValuePair<string, int>> into a Dictionary<string, int>
- How to make these 2 methods more Efficient
- .NET C# I want to get the IDs (1/multiple) from a data if it much the ID of another table
- Recursively Query XML with Linq
- Optimizing array iteration with nested Where (LINQ) clauses
- Linq to Excel with multiple tabs
- Selecting all child objects in Linq
- cannot get a timespan to add to a datetime
- How should I compare values in two lists?
- ServiceStack OrmLite "Like" Linq
- Linq code not working properly
- How to pass value by reference in C#?
- Not in clause two list<> objects
- Combine data gathered from different sources into one IEnumerable object
- Iterate over array with LINQ and use the value at each position