# System folders are protected

**URL:** <https://forum.xojo.com/t/system-folders-are-protected/73903>\
**Category:** Windows\
**Created:** [January 2, 2023, 12:02pm UTC](https://forum.xojo.com/t/system-folders-are-protected/73903 "2023-01-02T12:02:45Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![ChristopheDV](https://forum.xojo.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@ChristopheDV](https://forum.xojo.com/u/ChristopheDV)\
**Post date:** [January 2, 2023, 12:02pm UTC](https://forum.xojo.com/t/system-folders-are-protected/73903/1 "2023-01-02T12:02:45Z")

</div>

You may or may not aware that windows 11 has a protection that is called Randsom ware protection (Windows 10 has a similar protection but not the same).  
This blocks programs to write (and sometimes read) files on certain default folders (and more if the user has added custom folders too).

If you try to access those folders when this protection is enabled, you will get a NilObjectException crash (that cannot be catched). Even just linking it to a FolderItem will result in a crash.

These are the the default folders:

- `c:\Users\<username>\Documents`
- `c:\Users\Public\Documents`
- `c:\Users\<username>\Pictures`
- `c:\Users\Public\Pictures`
- `c:\Users\Public\Videos`
- `c:\Users\<username>\Videos`
- `c:\Users\<username>\Music`
- `c:\Users\Public\Music`
- `c:\Users\<username>\Favorites`

So keep that in mind.  
Also, atm this protection is disabled by default. But I did read Microsoft are considering this to be enabled by default.

On a side note: maybe Xojo Inc could add a way to catch the NilObjectException.

Here a link to the MS docs:

> **[Protect important folders from ransomware from encrypting your files with...](https://learn.microsoft.com/en-us/microsoft-365/security/defender-endpoint/controlled-folders?view=o365-worldwide)**
>
> Files in default folders can be protected from being changed by malicious apps. Prevent ransomware from encrypting your files.

---

<div class="post-metadata">

**Author:** ![Rick\_Araujo](https://forum.xojo.com/user_avatar/forum.xojo.com/rick_araujo/32/251_2.png) [@Rick\_Araujo](https://forum.xojo.com/u/Rick_Araujo)\
**Post date:** [January 2, 2023, 12:21pm UTC](https://forum.xojo.com/t/system-folders-are-protected/73903/2 "2023-01-02T12:21:02Z")

</div>

> [@ChristopheDV](#):
>
> Randsom ware protection

Ransomware protection. Both Win 10 and Win 11 have it.

> [@ChristopheDV](#):
>
> Xojo Inc could add a way to catch the NilObjectException.

I don’t get it. Things like these always worked:

```auto
Var nilFolder As FolderItem
Try
  MessageBox nilFolder.Exists.ToString // Nil Object Exception
Catch e As NilObjectException
  MessageBox "NOE was catched"
End

break

```

How to reproduce such non-catchable NOE ?

---

<div class="post-metadata">

**Author:** ![ChristopheDV](https://forum.xojo.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@ChristopheDV](https://forum.xojo.com/u/ChristopheDV)\
**Post date:** [January 2, 2023, 2:16pm UTC](https://forum.xojo.com/t/system-folders-are-protected/73903/3 "2023-01-02T14:16:35Z")

</div>

Try this with Ransomware enabled:

> var f as FolderItem  
> f = SpecialFolder.Movies.child(“test.txt”)
> 
> try  
> Var t As TextOutputStream = TextOutputStream.Create(f)  
> t.Close  
> catch  
> end

And you get this:

[![](https://forum.xojo.com/uploads/default/original/2X/9/9657f4a143763b6bdc08fb77a162753c2344b0a3.png)](https://postimages.org/)

---

<div class="post-metadata">

**Author:** ![Sascha\_S](https://forum.xojo.com/user_avatar/forum.xojo.com/sascha_s/32/22328_2.png) [@Sascha\_S](https://forum.xojo.com/u/Sascha_S)\
**Post date:** [January 2, 2023, 2:20pm UTC](https://forum.xojo.com/t/system-folders-are-protected/73903/4 "2023-01-02T14:20:28Z")

</div>

But you show us an IOException, no NilObjectException?

May it be possible that the test.txt File already exist or any other issue exists?

---

<div class="post-metadata">

**Author:** ![Greg\_O](https://forum.xojo.com/user_avatar/forum.xojo.com/greg_o/32/22785_2.png) [@Greg\_O](https://forum.xojo.com/u/Greg_O)\
**Post date:** [January 2, 2023, 3:25pm UTC](https://forum.xojo.com/t/system-folders-are-protected/73903/5 "2023-01-02T15:25:40Z")

</div>

Instead of just using `catch`, do you have different results if you specify the exception type ? Such as

```auto
Catch ex as RuntimeException

```

---

<div class="post-metadata">

**Author:** ![Rick\_Araujo](https://forum.xojo.com/user_avatar/forum.xojo.com/rick_araujo/32/251_2.png) [@Rick\_Araujo](https://forum.xojo.com/u/Rick_Araujo)\
**Post date:** [January 2, 2023, 4:28pm UTC](https://forum.xojo.com/t/system-folders-are-protected/73903/6 "2023-01-02T16:28:49Z")

</div>

That’s an **IOException** , error 2. No folder or file found.

So FolderItem is ok, SpecialFolder is ok.

But you can’t get a write permission there. The system hides/forbid things under some circumstances.

Here I do intercept it without problems.

 ![image](https://forum.xojo.com/uploads/default/original/2X/0/0a8920a5c98968b7b70504dc41b6a03002cf816c.png)

---

<div class="post-metadata">

**Author:** ![ChristopheDV](https://forum.xojo.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@ChristopheDV](https://forum.xojo.com/u/ChristopheDV)\
**Post date:** [January 3, 2023, 9:43am UTC](https://forum.xojo.com/t/system-folders-are-protected/73903/7 "2023-01-03T09:43:09Z")

</div>

> [@Greg\_O](#):
>
> `Catch ex as RuntimeException`

This does work.  
But it’s odd… just ‘Catch’ should be enough to catch any exception, not?

---

<div class="post-metadata">

**Author:** ![ChristopheDV](https://forum.xojo.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@ChristopheDV](https://forum.xojo.com/u/ChristopheDV)\
**Post date:** [January 3, 2023, 9:44am UTC](https://forum.xojo.com/t/system-folders-are-protected/73903/8 "2023-01-03T09:44:21Z")

</div>

> [@Sascha\_S](#):
>
> But you show us an IOException, no NilObjectException?
> 
> May it be possible that the test.txt File already exist or any other issue exists?

```auto
try
catch
end

```

Should catch any exception. But it seems, it doesn’t do that for this specific issue. Which is a bug imo.

---

<div class="post-metadata">

**Author:** ![Rick\_Araujo](https://forum.xojo.com/user_avatar/forum.xojo.com/rick_araujo/32/251_2.png) [@Rick\_Araujo](https://forum.xojo.com/u/Rick_Araujo)\
**Post date:** [January 3, 2023, 12:28pm UTC](https://forum.xojo.com/t/system-folders-are-protected/73903/9 "2023-01-03T12:28:57Z")

</div>

> [@ChristopheDV](#):
>
> Should catch any exception. But it seems, it doesn’t do that for this specific issue. Which is a bug imo.

Here it does work. The behavior shown is:

1. It Breaks on the line where the exception occurs. It does not fire any message box.
2. You resume the run and it continues without any problem, so the exception was discarded.

By the way: Xojo 2022R4.1 on Windows 10+

---

<div class="post-metadata">

**Author:** ![ChristopheDV](https://forum.xojo.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@ChristopheDV](https://forum.xojo.com/u/ChristopheDV)\
**Post date:** [January 3, 2023, 1:07pm UTC](https://forum.xojo.com/t/system-folders-are-protected/73903/10 "2023-01-03T13:07:55Z")

</div>

Did you enable Ransomware protection?

---

<div class="post-metadata">

**Author:** ![Rick\_Araujo](https://forum.xojo.com/user_avatar/forum.xojo.com/rick_araujo/32/251_2.png) [@Rick\_Araujo](https://forum.xojo.com/u/Rick_Araujo)\
**Post date:** [January 3, 2023, 1:10pm UTC](https://forum.xojo.com/t/system-folders-are-protected/73903/11 "2023-01-03T13:10:34Z")

</div>

Sure. When I disable it, it does not raise that catched exception.

RP disabled:

 ![image](https://forum.xojo.com/uploads/default/original/2X/9/92c9e7987714bbe2f5a8bc4c0f21a1780e56b49e.png)

RP Enabled:

 ![image](https://forum.xojo.com/uploads/default/original/2X/0/056f2ae972709df6d5d406dc1015e49ba8ad1cbb.png)

Then resumed:

 ![image](https://forum.xojo.com/uploads/default/original/2X/6/6caa3b8615dfc124b4d23fd91263ad0b715c00a7.png)

---

<div class="post-metadata">

**Author:** ![ChristopheDV](https://forum.xojo.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@ChristopheDV](https://forum.xojo.com/u/ChristopheDV)\
**Post date:** [January 3, 2023, 2:35pm UTC](https://forum.xojo.com/t/system-folders-are-protected/73903/12 "2023-01-03T14:35:14Z")

</div>

> [@Rick\_Araujo](#):
>
> By the way: Xojo 2022R4.1 on Windows 10+

I am running an older Xojo version for Windows. Will try the latest version.

---

<div class="post-metadata">

**Author:** ![Julia\_Truchsess](https://forum.xojo.com/user_avatar/forum.xojo.com/julia_truchsess/32/499_2.png) [@Julia\_Truchsess](https://forum.xojo.com/u/Julia_Truchsess)\
**Post date:** [January 3, 2023, 4:03pm UTC](https://forum.xojo.com/t/system-folders-are-protected/73903/13 "2023-01-03T16:03:56Z")

</div>

So is there a case where trying to access user\Documents or user\Desktop or user\AppData\Roaming for example, results in an NOE? Or is it always an I/O Exception? I have an NOE issues that only affects certain users (who I think may all be on Windows 11) and am wondering if it could be related.

---

<div class="post-metadata">

**Author:** ![ChristopheDV](https://forum.xojo.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@ChristopheDV](https://forum.xojo.com/u/ChristopheDV)\
**Post date:** [January 3, 2023, 4:33pm UTC](https://forum.xojo.com/t/system-folders-are-protected/73903/14 "2023-01-03T16:33:29Z")

</div>

> [@Julia\_Truchsess](#):
>
> user\Documents

user\Documents cannot be written to when Ransomware protection is enabled.  
See all folders that are protected by default in the original mail.

---

<div class="post-metadata">

**Author:** ![Julia\_Truchsess](https://forum.xojo.com/user_avatar/forum.xojo.com/julia_truchsess/32/499_2.png) [@Julia\_Truchsess](https://forum.xojo.com/u/Julia_Truchsess)\
**Post date:** [January 3, 2023, 5:10pm UTC](https://forum.xojo.com/t/system-folders-are-protected/73903/15 "2023-01-03T17:10:12Z")

</div>

> [@ChristopheDV](#):
>
> user\Documents cannot be written to when Ransomware protection is enabled

Yes, I understood that, but your original post said that it resulted in a NilObjectException, but your example screenshot showed an IOException, so I’m asking if an NOE ever does in fact occur.

---

<div class="post-metadata">

**Author:** ![Greg\_O](https://forum.xojo.com/user_avatar/forum.xojo.com/greg_o/32/22785_2.png) [@Greg\_O](https://forum.xojo.com/u/Greg_O)\
**Post date:** [January 3, 2023, 5:39pm UTC](https://forum.xojo.com/t/system-folders-are-protected/73903/16 "2023-01-03T17:39:24Z")

</div>

To be honest, I don’t use catch without specifying.

---

<div class="post-metadata">

**Author:** ![Michael\_Dettmer](https://forum.xojo.com/user_avatar/forum.xojo.com/michael_dettmer/32/22520_2.png) [@Michael\_Dettmer](https://forum.xojo.com/u/Michael_Dettmer)\
**Post date:** [January 14, 2023, 11:19am UTC](https://forum.xojo.com/t/system-folders-are-protected/73903/17 "2023-01-14T11:19:23Z")

</div>

Interesting discussion here. The question is not **if and how** to react on the occurance of a protected folder. The question should be:

**How can I write into that folder if it is protected?**

The answer is: Not at all. (If protection stays switched on)

My solution was to create a new directory in the “User” directory. This can be used without any problems. Microsoft: 🤡
