Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="getfolderpath.fs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//<snippet1>
// Sample for the Environment.GetFolderPath method
open System

printfn $"\nGetFolderPath: {Environment.GetFolderPath Environment.SpecialFolder.System}"

// This example produces the following results:
// GetFolderPath: C:\WINNT\System32
//</snippet1>
10 changes: 10 additions & 0 deletions snippets/fsharp/System/Environment/CommandLine/commandline.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//<snippet1>
open System

// Invoke this sample with an arbitrary set of command line arguments.
printfn $"\nCommandLine: {Environment.CommandLine}"
// The example displays output like the following:
// C:\>env0 ARBITRARY TEXT
//
// CommandLine: env0 ARBITRARY TEXT
//</snippet1>
10 changes: 10 additions & 0 deletions snippets/fsharp/System/Environment/CommandLine/fs.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="commandline.fs" />
</ItemGroup>
</Project>
18 changes: 18 additions & 0 deletions snippets/fsharp/System/Environment/CurrentDirectory/Vars1.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//<Snippet4>
open System
open System.IO

if Environment.OSVersion.Platform = PlatformID.Win32NT then
// Change the directory to %WINDIR%
Environment.CurrentDirectory <- Environment.GetEnvironmentVariable "windir"

let info = DirectoryInfo "."

printfn $"Directory Info: {info.FullName}"
else
printfn "This example runs on Windows only."
// The example displays output like the following on a .NET implementation running on Windows:
// Directory Info: C:\windows
// The example displays the following output on a .NET implementation on Unix-based systems:
// This example runs on Windows only.
//</Snippet4>
10 changes: 10 additions & 0 deletions snippets/fsharp/System/Environment/CurrentDirectory/fs.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="Vars1.fs" />
</ItemGroup>
</Project>
23 changes: 23 additions & 0 deletions snippets/fsharp/System/Environment/ExitCode/double.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module double

// <Snippet1>
open System
open System.Numerics

let ERROR_BAD_ARGUMENTS = 0xA0
let ERROR_ARITHMETIC_OVERFLOW = 0x216
let ERROR_INVALID_COMMAND_LINE = 0x667

let args = Environment.GetCommandLineArgs()
if args.Length = 1 then
Environment.ExitCode <- ERROR_INVALID_COMMAND_LINE
else
match BigInteger.TryParse args[1] with
| true, value ->
if value <= bigint Int32.MinValue || value >= bigint Int32.MaxValue then
Environment.ExitCode <- ERROR_ARITHMETIC_OVERFLOW
else
printfn $"Result: {value * 2I}"
| _ ->
Environment.ExitCode <- ERROR_BAD_ARGUMENTS
// </Snippet1>
27 changes: 27 additions & 0 deletions snippets/fsharp/System/Environment/ExitCode/double1.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module double1

// <Snippet2>
open System
open System.Numerics

let ERROR_SUCCESS = 0
let ERROR_BAD_ARGUMENTS = 0xA0
let ERROR_ARITHMETIC_OVERFLOW = 0x216
let ERROR_INVALID_COMMAND_LINE = 0x667

[<EntryPoint>]
let main _ =
let args = Environment.GetCommandLineArgs()
if args.Length = 1 then
ERROR_INVALID_COMMAND_LINE
else
match BigInteger.TryParse args[1] with
| true, value ->
if value <= bigint Int32.MinValue || value >= bigint Int32.MaxValue then
ERROR_ARITHMETIC_OVERFLOW
else
printfn $"Result: {value * 2I}"
ERROR_SUCCESS
| _ ->
ERROR_BAD_ARGUMENTS
// </Snippet2>
11 changes: 11 additions & 0 deletions snippets/fsharp/System/Environment/ExitCode/fs.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="double.fs" />
<Compile Include="double1.fs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//<snippet1>
// Sample for the Environment.ExpandEnvironmentVariables method
open System

let nl = Environment.NewLine

// <-- Keep this information secure! -->
let query = "My system drive is %SystemDrive% and my system root is %SystemRoot%"
let str = Environment.ExpandEnvironmentVariables query
printfn $"\nExpandEnvironmentVariables: {nl} {str}"

// This example produces the following results:
// ExpandEnvironmentVariables:
// My system drive is C: and my system root is C:\WINNT
//</snippet1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="expandenvironmentvariables.fs" />
</ItemGroup>
</Project>
18 changes: 18 additions & 0 deletions snippets/fsharp/System/Environment/FailFast/ff.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//<snippet1>
open System

let causeOfFailure = "A catastrophic failure has occurred."

// Assume your application has failed catastrophically and must
// terminate immediately. The try-finally block is not executed
// and is included only to demonstrate that instructions within
// try-catch blocks and finalizers are not performed.
try
Environment.FailFast causeOfFailure
finally
printfn "This finally block will not be executed."

// The example produces no output because the application is terminated.
// However, an entry is made in the Windows Application event log, and
// the log entry contains the text from the causeOfFailure variable.
//</snippet1>
10 changes: 10 additions & 0 deletions snippets/fsharp/System/Environment/FailFast/fs.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="ff.fs" />
</ItemGroup>
</Project>
10 changes: 10 additions & 0 deletions snippets/fsharp/System/Environment/GetCommandLineArgs/fs.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="getcommandlineargs.fs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//<snippet1>
open System

// Invoke this sample with an arbitrary set of command line arguments.
let arguments = Environment.GetCommandLineArgs()

String.concat ", " arguments
|> printfn "\nGetCommandLineArgs: %s"

// This example produces output like the following:
// C:\>GetCommandLineArgs ARBITRARY TEXT
//
// GetCommandLineArgs: GetCommandLineArgs, ARBITRARY, TEXT
// </snippet1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="getenvironmentvariableex1.fs" />
<Compile Include="gsev.fs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
module Example

open System

let mutable toDelete = false

// Check whether the environment variable exists.
let value =
let v = Environment.GetEnvironmentVariable "Test1"
// If necessary, create it.
if isNull v then
Environment.SetEnvironmentVariable("Test1", "Value1")
toDelete <- true
Environment.GetEnvironmentVariable "Test1"
else
v

// Display the value.
printfn $"Test1: {value}\n"

// Confirm that the value can only be retrieved from the process
// environment block if running on a Windows system.
if Environment.OSVersion.Platform = PlatformID.Win32NT then
printfn "Attempting to retrieve Test1 from:"
for enumValue in Enum.GetValues typeof<EnvironmentVariableTarget> do
let value = Environment.GetEnvironmentVariable("Test1", enumValue :?> EnvironmentVariableTarget)
printfn $""" {enumValue}: {if value <> null then "found" else "not found"}"""
printfn ""

// If we've created it, now delete it.
if toDelete then
Environment.SetEnvironmentVariable("Test1", null)
// Confirm the deletion.
if Environment.GetEnvironmentVariable "Test1" |> isNull then
printfn "Test1 has been deleted."
// The example displays the following output if run on a Windows system:
// Test1: Value1
//
// Attempting to retrieve Test1 from:
// Process: found
// User: not found
// Machine: not found
//
// Test1 has been deleted.
//
// The example displays the following output if run on a Unix-based system:
// Test1: Value1
//
// Test1 has been deleted.
Loading