I am trying to check if a file path exists on a server so that it can be worked with. I am currently using localhost as the server for testing purposes. I tried if (File.Exists(filePath1)) it finds the path, but it looks for the file locally without accessing the server.

How can check if filePath1 exists in server1? I am using the following parameter values to test the solution before it gets deployed.

Parameter values in class ParmNames:

filePath1 = "C:\\Users\\smithj\\Documents\\file.txt"

server1 = "localhost"

public class class1
{
    FileRules putFileRH = new FileRules();
    List<IRuleParameter> FileParms = new List<IRuleParameter>();

    private bool Method1()
    {       
        FileParms.Add(new RuleParms(ParmNames.SourceServer, server1.ToString(), string.Empty));
        FileParms.Add(new RuleParms(ParmNames.FileName, filePath1, string.Empty));

        foreach (var server in FileParms)   //not sure if this actually goes through each server or through each parm  in FileParms
        {
            //if (File.Exists(filePath1))         first attempt
            if (File.Exists(server.ValueEnvironment))   //this attempt doesn't find the file
            {
                ...
                return true;
            }
            else
            {
                ...
                return false;
            }
            return true;
        }
    }
}

IRuleParameter interface:

public interface IRuleParameter
{
    string Name { get; }
    string ValueEnvironment { get; }
    string ValueType { get; }
}
有帮助吗?

解决方案 2

Looks like you want to find all rules with a given name and check condition for some /all of such rules. Use Where to filter out rules you want and Any or All depending on your needs to check condition... With current sample Any should be enough:

var fileExists = FileParms
   .Where(r => r.Name == ParmNames.FileName) // find all "File names"
   .Any(r =>File.Exists(r.ValueEnvironment) // check if any of the files exist

其他提示

Here you should check for server.FileName (or whatever is your property name in the RuleParms class)

if (File.Exists(server.ValueEnvironment))  
{
      return true;
}

In your foreach loop server represents your current RuleParms item.So you need to check it's property.FileName should be your property name that holds the filePath1 value.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top