Click here to Skip to main content
14,775,705 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Currently i'm trying to create a directory:

string appDocsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
string lcvDocsPath = Path.Combine(appDocsPath, "latex_curriculum_vitae", "Attachements", "Letter_of_Application");
Directory.CreateDirectory(lcvDocsPath);


But sadly i'm getting:

System.IO.FileNotFoundException: "Could not find file 'C:\Users\Sasch\OneDrive\Dokumente\latex_curriculum_vitae\Attachements\Letter_of_Application'."


But why this error occurs?

Edit: I found it out. In the same time as my program tries to add the mentioned directory, the Windows Defender comes up, and i have to manually allow the access to the Documents directory.
Is it possible to bypass this?

What I have tried:

I looked there. I used it in the same way.
The same says the documentation
Posted
Updated 18-Sep-20 1:23am
v2
Comments
Richard Deeming 18-Sep-20 5:58am
   
Directory.CreateDirectory[^] is not documented as throwing a FileNotFoundException. Are you sure that's the line that's throwing the exception? Have you checked which parts of the path exist?
Sascha Manns 18-Sep-20 6:05am
   
I found it out. In the same time as my program tries to add the mentioned directory, the Windows Defender comes up, and i have to manually allow the access to the Documents directory.
Is it possible to bypass this?
Richard MacCutchan 18-Sep-20 7:48am
   
That should not happen if you own the OneDrive directory. Check the permissions to see that your userid has full access.
Sascha Manns 18-Sep-20 7:59am
   
I checked it. It looks like i have added the OneDrive/Documents directory to the list of Defenders monitored directories list. So Defender blocks it.
Is this feature enabled in a fresh system?

1 solution

I am not sure if Windows Defender will try to prevent folder creation in the Documents/Downloads folder; it should require elevated access or protection when working in Program Files or other sensitive folders.

One thing I would like you to think about is, if Windows Defender can prevent you from creating the folder, it will do the same for your users' machines. Are you okay with your process being halted for this, and do you think your users will do the same; create or allow for a directory themselves?
Quote:
C:\Users\Sasch\OneDrive\Dokumente <-- the OneDrive part here
Lastly, you are creating the folder not in the machine's Documents folder, but in OneDrive. I think you should see why your PC is creating the directory on OneDrive, as that is why Windows Defender or other programs are denying access. It might be because allowing such an operation will cost the user in their cloud storage which Microsoft needs to prevent.

If this doesn't work, try using the Windows variables (%USERPROFILE%) to access the documents directory on the PC; not the network.

Environment.ExpandEnvironmentVariables(String) Method (System) | Microsoft Docs[^]

Edit
Oh, and unless you really want to allow the user to access/modify/process the data outside your application, why not use the app's local storage for a folder? I would recommend using LocalApplicationData as the entrypoint for such IO operations.

Try this enumeration to find out other possible folder names that your app can access and use, Environment.SpecialFolder Enum (System) | Microsoft Docs[^]
   
v3
Comments
Richard MacCutchan 18-Sep-20 7:47am
   
Microsoft (in their wisdom) changed Windows so that My Documents is on OneDrive by default these days. And that should not trigger defender since it belongs to the user. In fact, I just tested this myself and it works fine creating the directory on OneDrive.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900