Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

file IO, is this a bug in Powershell?

I have the following code in Powershell

$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")
 }

I keep getting error:

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"."

I tried the equivalent in 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");
    }

it works fine!

What's wrong with my powershell script? BTW I am running on powershell

Major  Minor  Build  Revision
-----  -----  -----  --------
3      2      0      2237
like image 214
user1866880 Avatar asked Jan 12 '13 14:01

user1866880


People also ask

How do you check if a file exists in a directory in PowerShell?

The Test-Path Cmdlet $Folder = 'C:\Windows' "Test to see if folder [$Folder] exists" if (Test-Path -Path $Folder) { "Path exists!" } else { "Path doesn't exist." } This is similar to the -d $filepath operator for IF statements in Bash. True is returned if $filepath exists, otherwise False is returned.

How do I check if a file is empty in PowerShell?

To check if the file is empty using PowerShell, we can use the string method called IsNullorWhiteSpace(). This method provides result true if the file is empty or only contains the white spaces otherwise false.


3 Answers

Yet another way could be to enclose the enums in parens:

$wStream = new-object IO.FileStream $filePath, ([System.IO.FileMode]::Append), `
    ([IO.FileAccess]::Write), ([IO.FileShare]::Read)
like image 150
mousio Avatar answered Nov 01 '22 23:11

mousio


When using the New-Object cmdlet and the target type constructor takes in parameters, you should either use the -ArgumentList parameter (of New-Object) or wrap the parameters in parenthesis - I prefer to wrap my constructors with parens:

# 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 online help: http://go.microsoft.com/fwlink/?LinkID=113355

like image 43
Goyuix Avatar answered Nov 02 '22 00:11

Goyuix


Another way would be to use just the name of the value and let PowerShell cast it to the target type:

New-Object IO.FileStream $filePath ,'Append','Write','Read'
like image 38
Shay Levy Avatar answered Nov 01 '22 22:11

Shay Levy