如何从 cmd 运行 一个程序
How to run a program from cmd
我希望能够在任何文本文件上 运行 它,而不必在代码中指定路径。有人可以帮忙吗?谢谢
open System.IO
type Data() =
member x.Read() =
use stream = new StreamReader @"D:\Searcher.txt"
let mutable valid = true
printfn "Please enter the number you want to search for: "
let mutable x = System.Console.ReadLine()
let mutable lines = 1
while (valid ) do
let line = stream.ReadLine()
if (line = null) then
valid <- false
else if (line = x) then
printfn "String found: %s" x
printfn "Found on: Line %i " lines
valid <- false
else
lines <- lines + 1
let data = Data()
data.Read()
System.Console.ReadKey() |> ignore
您可以通过三种方式将文件名作为参数传递。在已编译的程序中使用 argv
,如果通过 fsi 执行则使用 fsi.CommandLineArgs
,最后通过 Console.Readline
获取文件名参数,与获取要搜索的字符串完全相同。您不需要定义 class 来执行此操作,因此我将其删除并使其成为一个函数。您还可以简化阅读函数中的代码,使其不那么命令。
版本 1
在编译程序中 argv
作为字符串列表传递给 main:
open System.IO
[<EntryPoint>]
let main argv =
let fName = argv.[0] // get the file name as the first argument
let readData (fName:string) =
use stream = new StreamReader(fName)
let mutable valid = true
printfn "Please enter the number you want to search for: "
let mutable x = System.Console.ReadLine()
let mutable lines = 1
while (valid ) do
let line = stream.ReadLine()
if (line = null) then
valid <- false
else if (line = x) then
printfn "String found: %s" x
printfn "Found on: Line %i " lines
valid <- false
else
lines <- lines + 1
readData fName
System.Console.ReadKey() |> ignore
0 // return an integer exit code
您会 运行 在编译应用程序后,像这样:
.\ConsoleApplication13.exe "c:\tmp\SearchText.txt"
版本 2
如果你不想编译而只是 运行 通过 fsi:
open System.IO
let fName = fsi.CommandLineArgs.[1] // you need the second item from the argument list,
//because the first one is the script name itself
let readData (fName:string) =
use stream = new StreamReader(fName)
let mutable valid = true
printfn "Please enter the number you want to search for: "
let mutable x = System.Console.ReadLine()
let mutable lines = 1
while (valid ) do
let line = stream.ReadLine()
if (line = null) then
valid <- false
else if (line = x) then
printfn "String found: %s" x
printfn "Found on: Line %i " lines
valid <- false
else
lines <- lines + 1
readData fName
System.Console.ReadKey() |> ignore
0 // return an integer exit code
你会运行这样:
Fsi.exe .\Script1.fsx "C:\tmp\SearchText.txt"
对于版本 3,我不会详细说明,因为您已经在使用 Console.Readline
来获取搜索字符串。您可以用完全相同的方式获取文件名。
另外 x
不需要是可变的。
我希望能够在任何文本文件上 运行 它,而不必在代码中指定路径。有人可以帮忙吗?谢谢
open System.IO
type Data() =
member x.Read() =
use stream = new StreamReader @"D:\Searcher.txt"
let mutable valid = true
printfn "Please enter the number you want to search for: "
let mutable x = System.Console.ReadLine()
let mutable lines = 1
while (valid ) do
let line = stream.ReadLine()
if (line = null) then
valid <- false
else if (line = x) then
printfn "String found: %s" x
printfn "Found on: Line %i " lines
valid <- false
else
lines <- lines + 1
let data = Data()
data.Read()
System.Console.ReadKey() |> ignore
您可以通过三种方式将文件名作为参数传递。在已编译的程序中使用 argv
,如果通过 fsi 执行则使用 fsi.CommandLineArgs
,最后通过 Console.Readline
获取文件名参数,与获取要搜索的字符串完全相同。您不需要定义 class 来执行此操作,因此我将其删除并使其成为一个函数。您还可以简化阅读函数中的代码,使其不那么命令。
版本 1
在编译程序中 argv
作为字符串列表传递给 main:
open System.IO
[<EntryPoint>]
let main argv =
let fName = argv.[0] // get the file name as the first argument
let readData (fName:string) =
use stream = new StreamReader(fName)
let mutable valid = true
printfn "Please enter the number you want to search for: "
let mutable x = System.Console.ReadLine()
let mutable lines = 1
while (valid ) do
let line = stream.ReadLine()
if (line = null) then
valid <- false
else if (line = x) then
printfn "String found: %s" x
printfn "Found on: Line %i " lines
valid <- false
else
lines <- lines + 1
readData fName
System.Console.ReadKey() |> ignore
0 // return an integer exit code
您会 运行 在编译应用程序后,像这样:
.\ConsoleApplication13.exe "c:\tmp\SearchText.txt"
版本 2
如果你不想编译而只是 运行 通过 fsi:
open System.IO
let fName = fsi.CommandLineArgs.[1] // you need the second item from the argument list,
//because the first one is the script name itself
let readData (fName:string) =
use stream = new StreamReader(fName)
let mutable valid = true
printfn "Please enter the number you want to search for: "
let mutable x = System.Console.ReadLine()
let mutable lines = 1
while (valid ) do
let line = stream.ReadLine()
if (line = null) then
valid <- false
else if (line = x) then
printfn "String found: %s" x
printfn "Found on: Line %i " lines
valid <- false
else
lines <- lines + 1
readData fName
System.Console.ReadKey() |> ignore
0 // return an integer exit code
你会运行这样:
Fsi.exe .\Script1.fsx "C:\tmp\SearchText.txt"
对于版本 3,我不会详细说明,因为您已经在使用 Console.Readline
来获取搜索字符串。您可以用完全相同的方式获取文件名。
另外 x
不需要是可变的。