2014-01-23 25 views
0

我正在嘗試檢查服務器上是否存在文件路徑,以便它可以使用。我目前使用本地主機作爲服務器進行測試。我試過if (File.Exists(filePath1))它找到路徑,但它在本地查找文件而無需訪問服務器。如何使用parm值檢查文件是否存在?

如何檢查是否存在 filePath1?我使用以下參數值在部署之前測試解決方案。類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接口:

public interface IRuleParameter 
{ 
    string Name { get; } 
    string ValueEnvironment { get; } 
    string ValueType { get; } 
} 
+0

請提供最少的代碼演示捲入問題的所有作品(如'IRuleParameter') –

回答

1

看起來像你想找到一個給定的名稱的所有規則和檢查條件的一些/所有的這些規則。使用Where篩選出你想要的規則和AnyAll根據您的需求來檢查條件......隨着電流採樣Any應該足夠:

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 
2

在這裏,你應該檢查server.FileName(或任何你是在RuleParms R類屬性名)

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

在你的foreach循環server表示當前RuleParms item.So你需要檢查它的性能。 FileName應該是您的財產名稱,其值爲filePath1

+0

它給了我下面的錯誤:'「IRuleParameter不包含定義文件名」' –

+0

我告訴你,你應該改變it.What是你的財產的名字?提供你的'RuleParms'類或'IRuleParameter'接口 –

+0

@JohnSmith它應該是'ValueEnvironment'根據你的參數順序。但我不確定。 –

相關問題