2013-01-12 36 views
6

Tôi có đoạn code sau trong Powershelltệp IO, đây có phải là lỗi trong Powershell không?

$filePath = "C:\my\programming\Powershell\output.test.txt" 

try 
{ 
    $wStream = new-object IO.FileStream $filePath, [System.IO.FileMode]::Append, [IO.FileAccess]::Write, [IO.FileShare]::Read 

    $sWriter = New-Object System.IO.StreamWriter $wStream 

    $sWriter.writeLine("test") 
} 

tôi tiếp tục nhận được lỗi:

Cannot convert argument "1", with value: "[IO.FileMode]::Append", for "FileStream" to type "System.IO.FileMode": "Cannot convert value "[IO.FileMode]::Append" to type "System.IO.FileMode" due to invalid enumeration values. Specify one of the following enumeration values and try again. The possible enumeration values are "CreateNew, Create, Open, OpenOrCreate, Truncate, Append"."

tôi đã cố gắng tương đương trong C#,

FileStream fStream = null; 
    StreamWriter stWriter = null; 

    try 
    { 
     fStream = new FileStream(@"C:\my\programming\Powershell\output.txt", FileMode.Append, FileAccess.Write, FileShare.Read); 
     stWriter = new StreamWriter(fStream); 
     stWriter.WriteLine("hahha"); 
    } 

nó hoạt động tốt!

Có gì sai với tập lệnh PowerShell của tôi? BTW Tôi đang chạy trên PowerShell

Major Minor Build Revision 
----- ----- ----- -------- 
3  2  0  2237 

Trả lời

16

Một cách khác sẽ được sử dụng chỉ là tên của giá trị và để cho PowerShell đúc nó vào loại mục tiêu:

New-Object IO.FileStream $filePath ,'Append','Write','Read' 
+0

Yup, đó là cách tiếp cận tôi cũng sẽ làm. –

0

Nếu mục tiêu của bạn là viết vào một logfile hoặc văn bản tập tin, sau đó bạn có thể thử các lệnh hỗ trợ trong PowerShell để đạt được điều này?

Get-Help Out-File -Detailed 
4

Khi sử dụng New-Object cmdlet và các loại mục tiêu xây dựng mất trong các thông số, bạn nên thể sử dụng -ArgumentList tham số (New-Object) hoặc quấn các thông số trong ngoặc đơn - Tôi thích quấn constructors tôi với dấu ngoặc:

# setup some convenience variables to keep each line shorter 
$path = [System.IO.Path]::Combine($Env:TEMP,"Temp.txt") 
$mode = [System.IO.FileMode]::Append 
$access = [System.IO.FileAccess]::Write 
$sharing = [IO.FileShare]::Read 

# create the FileStream and StreamWriter objects 
$fs = New-Object IO.FileStream($path, $mode, $access, $sharing) 
$sw = New-Object System.IO.StreamWriter($fs) 

# write something and remember to call to Dispose to clean up the resources 
$sw.WriteLine("Hello, PowerShell!") 
$sw.Dispose() 
$fs.Dispose() 

New-Object cmdlet trợ giúp trực tuyến: http://go.microsoft.com/fwlink/?LinkID=113355

+0

Bạn không kém có thể cũng bỏ qua các dấu ngoặc, bởi vì nó chỉ hy vọng một mảng đối tượng, tùy trước bởi '-ArgumentList'. Parens của bạn chỉ làm cho nó một biểu hiện riêng biệt; đúng không? – mousio

+3

Parens xung quanh một mảng trong PowerShell thực sự là một no-op ngoại trừ hoạt động như một dấu tách dấu hiệu. Tôi tránh cú pháp này 'new-object (arg, arg, ...)' bởi vì nó đánh lừa bạn tin rằng đây chỉ là một hàm tạo C# khi nó thực sự không. Nó thực sự ít gõ để sử dụng 'new-object arg, arg, ...'. Xin lỗi vì là người theo dõi nhưng tôi đã trả lời quá nhiều câu hỏi về lý do tại sao 'MyPowerShellFunctionThatTakesThreeParameters (1,2,3)' không hoạt động. :-) –

2

Tuy nhiên, một cách khác có thể gửi kèm theo enums trong dấu ngoặc:

$wStream = new-object IO.FileStream $filePath, ([System.IO.FileMode]::Append), ` 
    ([IO.FileAccess]::Write), ([IO.FileShare]::Read) 
Các vấn đề liên quan