Folderitem write issue

Hello,
I have a locking issue, with a file that i need to lock :smiley:

My app has a ops log which needs to be user accessible, if the user has the file open i cannot write to it and the app crashes. Folderitem.writeable seems to cover only permissions, not being able to write it without crashing.

What is the best way to check(adding and removing a 1 with a binarystream?) if i can write to the file(without running into io crashes)?
Thanks!

Which OS?

Targets are mac os and Windows(mostly Windows)

Your code? (However some cases are not recoverable)

It shouldn’t be crashing, it should throw an exception. Use Try/Catch when you write to it and catch the exception.

Yes Tim.
But you could avoid the exception (not always but at least you can try to avoid it)

This is the shared method used to write the strings, using a parameter called filestring(string).

[code]Dim fileStream As TextOutputStream
Dim FieldsString As String
Dim FileName As String
Dim FileTime as New Date
Dim CSVf as New Folderitem
Dim FileRow as String = 0

Filename = “Export-” + FileTime.SQLDate + “.csv”

CSVf = GetFolderItem(Filename)

FieldsString = “Sample data header”

If CSVf.Exists = true Then
filerow=1
end if

if filerow = 0 then
'Initialize the file
fileStream = TextOutputStream.Create(CSVf)
fileStream.WriteLine(FieldsString)
else
'Append data to file
fileStream = TextOutputStream.Append(CSVf)
end if
fileStream.WriteLine(filestring)
filerow = filerow + 1
fileStream.Close

Exception err As IOException
MsgBox(FileLocked)
RepeatCheckBox.State = Checkbox.CheckedStates.UnChecked
Timer1.Enabled = False
exit[/code]

So you’re already catching the exception. Is it actually crashing? You are effectively checking if it’s writable and displaying a file locked message. What more do you want?

Oh, and the “exit” at the end of your code isn’t necessary.

Try to use the new framework
I’ve a log function like this and append with the new framework doesn’t raise the exception (in most cases, OS don’t work always in a predictable way)

use a try/catch is more readable and usable (personal opinion)

Really? That seems like a regression to me.

No, IO systems can accept that a file is opened in a not exclusive way.

Never handled exceptions before :slight_smile:
I Just dumped a codeblock lifted from a quick Google search, so I didn’t know if it was the correct way. If it is, yay less time to waste on this thing :smiley:

It is correct.