2009-04-10 63 views

回答

12
<?xml version="1.0" encoding="utf-8" ?> 
<project xmlns="http://nant.sf.net/release/0.86-beta1/nant.xsd" 
     name="Company.Portal.Domain" default="GetFile"> 
    <call target="GetFile" /> 
    <target name="GetFile"> 
    <echo message="Retrieving file contents"/> 
    <property name="file.contents" value="0" /> 
    <loadfile file="file.txt" property="file.contents" /> 
    <property name="file.contents" value="${string::trim(file.contents)}" /> 
    <echo message="contents of file is ${file.contents}"/> 
    </target> 
</project> 

当然,如果你愿意,你可以跳过线6,9和10。 [编辑]

<if test="${file.contents=='someValue'}"> 
    <echo>Some value found</echo> 
</if> 

this link

[EDIT2]

获取全部细节既然你想要得到的文本文件中的第三行的值,那么做到这一点

<?xml version="1.0"?> 
<project name="Read3rdLine" default="main"> 
    <property name="myInt" value="0"/> 
    <property name="x" value="0"/> 
    <property name="LineToRead" value="3"/> 

    <target name="main" description="compiles the source code"> 
    <property name="i" value="0"/> 
    <foreach item="Line" in="file.txt" property="x" trim="Both"> 
     <property name="i" value="${int::parse(i) + 1}"/> 
     <if test="${i==LineToRead}"> 
      <property name="myInt" value="${x}"/> 
     </if> 
    </foreach> 
    <echo>found ${myInt} at line ${LineToRead}</echo> 
    </target> 
</project> 
+0

多谢内(?) binoj.Just得到了另一个疑问??我对nant真的很陌生。因此,我在这个file.txt中有一个值,它是从上面的代码中看到的,现在我想将它与一个固定值进行比较并查找验证。如果这个值大或小,我想做一些checks.CanüPLZ帮助? – Maddy 2009-04-10 12:45:36

0

我发现使用正则表达式更加灵活,因为它不依赖于一行来处于特定的位置并且更易于编码。

<loadfile file="${filename}" property="assemblyInfo" /> 
<regex input="${assemblyInfo}" pattern="(?'assemblyVersion'AssemblyVersion[0-9.()&quot;]+)" /> 

的语法是有点奇怪,但2号线的结果是填充属性调用assemblyVersion与正则表达式的分组模式相匹配的字符串:一切