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
10 changes: 10 additions & 0 deletions snippets/fsharp/System.IO/File/AppendAllLines/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="program.fs" />
</ItemGroup>
</Project>
36 changes: 36 additions & 0 deletions snippets/fsharp/System.IO/File/AppendAllLines/program.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module program
// <Snippet1>
open System
open System.IO

let dataPath = @"c:\temp\timestamps.txt"

let createSampleFile () =
let timeStamp = DateTime(1700, 1, 1)

use sw = new StreamWriter(dataPath)

for i = 0 to 499 do
let ts1 = timeStamp.AddYears i
let ts2 = ts1.AddMonths i
let ts3 = ts2.AddDays i
ts3.ToLongDateString() |> sw.WriteLine

createSampleFile ()

let julyWeekends =
File.ReadLines dataPath
|> Seq.filter (fun line ->
(line.StartsWith "Saturday"
|| line.StartsWith "Sunday")
&& line.Contains "July")

File.WriteAllLines(@"C:\temp\selectedDays.txt", julyWeekends)

let marchMondays =
File.ReadLines dataPath
|> Seq.filter (fun line -> line.StartsWith "Monday" && line.Contains "March")

File.AppendAllLines(@"C:\temp\selectedDays.txt", marchMondays)

// </Snippet1>
26 changes: 26 additions & 0 deletions snippets/fsharp/System.IO/File/AppendAllText/AllText.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module AllText
//<snippet00>
open System
open System.IO

let path = @"c:\temp\MyTest.txt"

// This text is added only once to the file.
if File.Exists path |> not then
// Create a file to write to.
let createText =
"Hello and Welcome" + Environment.NewLine

File.WriteAllText(path, createText)

// This text is always added, making the file longer over time
// if it is not deleted.
let appendText =
"This is extra text" + Environment.NewLine

File.AppendAllText(path, appendText)

// Open the file to read from.
let readText = File.ReadAllText path
printfn $"{readText}"
//</snippet00>
27 changes: 27 additions & 0 deletions snippets/fsharp/System.IO/File/AppendAllText/AllText1.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module AllText1
//<snippet00>
open System
open System.IO
open System.Text

let path = @"c:\temp\MyTest.txt"

// This text is added only once to the file.
if File.Exists path |> not then
// Create a file to write to.
let createText =
"Hello and Welcome" + Environment.NewLine

File.WriteAllText(path, createText, Encoding.UTF8)

// This text is always added, making the file longer over time
// if it is not deleted.
let appendText =
"This is extra text" + Environment.NewLine

File.AppendAllText(path, appendText, Encoding.UTF8)

// Open the file to read from.
let readText = File.ReadAllText path
printfn $"{readText}"
//</snippet00>
11 changes: 11 additions & 0 deletions snippets/fsharp/System.IO/File/AppendAllText/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="AllText.fs" />
<Compile Include="AllText1.fs" />
</ItemGroup>
</Project>
32 changes: 32 additions & 0 deletions snippets/fsharp/System.IO/File/AppendText/file_appendtext.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module file_appendtext
// <Snippet1>
open System.IO

let path = @"c:\temp\MyTest.txt"

// This text is added only once to the file.
if File.Exists path |> not then
// Create a file to write to.
use sw = File.CreateText path
sw.WriteLine "Hello"
sw.WriteLine "And"
sw.WriteLine "Welcome"

// This text is always added, making the file longer over time
// if it is not deleted.
do
use sw = File.AppendText path
sw.WriteLine "This"
sw.WriteLine "is Extra"
sw.WriteLine "Text"

// Open the file to read from.
do
use sr = File.OpenText path

let mutable s = sr.ReadLine()

while isNull s |> not do
printfn $"{s}"
s <- sr.ReadLine()
// </Snippet1>
10 changes: 10 additions & 0 deletions snippets/fsharp/System.IO/File/AppendText/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="file_appendtext.fs" />
</ItemGroup>
</Project>
10 changes: 10 additions & 0 deletions snippets/fsharp/System.IO/File/Copy/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="program.fs" />
</ItemGroup>
</Project>
44 changes: 44 additions & 0 deletions snippets/fsharp/System.IO/File/Copy/program.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
open System.IO

// <Snippet1>
let sourceDir = @"c:\current"
let backupDir = @"c:\archives\2008"

try
let picList = Directory.GetFiles(sourceDir, "*.jpg")
let txtList = Directory.GetFiles(sourceDir, "*.txt")

// Copy picture files.
for f in picList do
// Remove path from the file name.
let fName = f.Substring(sourceDir.Length + 1)

// Use the Path.Combine method to safely append the file name to the path.
// Will overwrite if the destination file already exists.
File.Copy(Path.Combine(sourceDir, fName), Path.Combine(backupDir, fName), true)

// Copy text files.
for f in txtList do
// Remove path from the file name.
let fName = f.Substring(sourceDir.Length + 1)

try
// Will not overwrite if the destination file already exists.
File.Copy(Path.Combine(sourceDir, fName), Path.Combine(backupDir, fName))

// Catch exception if the file was already copied.
with
| :? IOException as copyError -> printfn $"{copyError.Message}"

// Delete source files that were copied.
for f in txtList do
File.Delete f

for f in picList do
File.Delete f

// Catch exception if the file was already copied.
with
| :? DirectoryNotFoundException as dirNotFound -> printfn $"{dirNotFound.Message}"

// </Snippet1>
27 changes: 27 additions & 0 deletions snippets/fsharp/System.IO/File/Create/file create1.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module filecreate1

// <Snippet1>
open System.IO
open System.Text

let path = @"c:\temp\MyTest.txt"

// Create the file, or overwrite if the file exists.
do
use fs = File.Create path

let info =
UTF8Encoding(true)
.GetBytes "This is some text in the file."
// Add some information to the file.
fs.Write(info, 0, info.Length)

// Open the stream and read it back.
do
use sr = File.OpenText path
let mutable s = sr.ReadLine()

while isNull s |> not do
printfn $"{s}"
s <- sr.ReadLine()
// </Snippet1>
27 changes: 27 additions & 0 deletions snippets/fsharp/System.IO/File/Create/file create2.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module filecreate2

// <Snippet1>
open System.IO
open System.Text

let path = @"c:\temp\MyTest.txt"

// Create the file, or overwrite if the file exists.
do
use fs = File.Create(path, 1024)

let info =
UTF8Encoding(true)
.GetBytes "This is some text in the file."
// Add some information to the file.
fs.Write(info, 0, info.Length)

// Open the stream and read it back.
do
use sr = File.OpenText path
let mutable s = sr.ReadLine()

while isNull s |> not do
printfn $"{s}"
s <- sr.ReadLine()
// </Snippet1>
11 changes: 11 additions & 0 deletions snippets/fsharp/System.IO/File/Create/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="file create1.fs" />
<Compile Include="file create2.fs" />
</ItemGroup>
</Project>
22 changes: 22 additions & 0 deletions snippets/fsharp/System.IO/File/CreateText/file createtext.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module filecreatetext

// <Snippet1>
open System.IO

let path = @"c:\temp\MyTest.txt"

if File.Exists path |> not then
// Create a file to write to.
use sw = File.CreateText path
sw.WriteLine "Hello"
sw.WriteLine "Welcome"

// Open the file to read from.
do
use sr = File.OpenText path
let mutable s = sr.ReadLine()

while isNull s |> not do
printfn $"{s}"
s <- sr.ReadLine()
// </Snippet1>
10 changes: 10 additions & 0 deletions snippets/fsharp/System.IO/File/CreateText/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="file createtext.fs" />
</ItemGroup>
</Project>
10 changes: 10 additions & 0 deletions snippets/fsharp/System.IO/File/Decrypt/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="sample.fs" />
</ItemGroup>
</Project>
23 changes: 23 additions & 0 deletions snippets/fsharp/System.IO/File/Decrypt/sample.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//<SNIPPET1>
open System.IO

// Encrypt a file.
let addEncryption fileName = File.Encrypt fileName

// Decrypt a file.
let removeEncryption fileName = File.Decrypt fileName

let fileName = "test.xml"

printfn $"Encrypt {fileName}"

// Encrypt the file.
addEncryption fileName

printfn $"Decrypt {fileName}"

// Decrypt the file.
removeEncryption fileName

printfn "Done"
//</SNIPPET1>
10 changes: 10 additions & 0 deletions snippets/fsharp/System.IO/File/Exists/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="program.fs" />
</ItemGroup>
</Project>
11 changes: 11 additions & 0 deletions snippets/fsharp/System.IO/File/Exists/program.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
open System.IO

// <Snippet1>
let curFile = @"c:\temp\test.txt"

printfn
$"""{if File.Exists curFile then
"File exists."
else
"File does not exist."}"""
// </Snippet1>
10 changes: 10 additions & 0 deletions snippets/fsharp/System.IO/File/GetAccessControl/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>net48</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="sample.fs" />
</ItemGroup>
</Project>
Loading