When using IO.File.Copy in a vb.net application which is sheduled using the sheduled tasks is resetting (or removing?) permissions to that file

Aushyro 0 Reputation points
2025-03-04T11:49:57.47+00:00

Hi all,

I have following code snippet:

IO.File.Copy(SourceFilePath, DestinationFilePath, OverWrite)

It can be any file on any network drive on the network.

However, my application is sheduled using the sheduled tasks MMC addon ON A DIFFERENT SERVER. My application runs ALWAYS FINE when I start it MANUALLY on my development PC OR the server where the application is supposed to be running. When I launch it automatically via the scheduled tasks, It ONLY runs fine when the user who created the scheduled task is logged in. It does not matter if the checkbox "Run wether the user is logged in or not" inside the sheduled tasks is checked.... When the user on that server is logged out, then the application won't run anymore. The reason WHY that user is logged out is due to server restarts AND/OR Windows updates. The ACTUAL cause of this issue is that when the user IS NOT LOGGED IN on that server is that there are some issues with the permissions of said copied file. So, YES the copy file command itself runs fine, but when I want to access that file it throws an error: "Access denied" (Error code: 5) exception... Again: this error does not throw when I run my programm manually OR when the user is logged in when the Scheduled task is starting... What can I do to resolve this issue??

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,793 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Jiachen Li-MSFT 33,701 Reputation points Microsoft External Staff
    2025-03-04T13:11:19.5466667+00:00

    Hi @Aushyro ,

    Make sure the scheduled task is configured to run under a user account with sufficient permissions. Specifically:

    • Open Task Scheduler (taskschd.msc).
    • Find your task and open its properties.
    • In the General tab:
      • Ensure "Run whether the user is logged on or not" is checked.
      • Use an account that has full control over the source and destination files.
      • If possible, try running it as a local administrator or a service account that does not require login.
    • In the Security Options section:
      • Click Change User or Group and enter a user that has full network access.
      • Ensure the account has "Log on as a batch job" permission (set via gpedit.msc → Local Policies → User Rights Assignment).
    • Ensure "Do not store password" is NOT checked, as this can restrict access to network resources.

    Best Regards.

    Jiachen Li


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. Aushyro 0 Reputation points
    2025-03-05T17:46:51.0333333+00:00

    Hello, thank you for your answer :)

    1 thing you have posted above I did not do:

    "Use an account that has full control over the source and destination files."

    Especially the source file I did not check the permissions on, so I made sure that the account has full control on both the security tab and the share permissions.

    However, the destination file does not exist yet. I cannot set permissions on them.

    After setting full control, it still does not work.

    Running it as a local administrator or a service account does not work cause they are not able to access the network resources of the source file.

    I have been trying to solve this for a few days already. As this is a high-risk application I went on and re-writing the code to access the files directly instead of copying them. The issue with accessing them directly over the network share is that other users experience performance issues on their end. But also i'm using a very slow VPN connection. When my tool runs and I copy the file locally before processing it, it just takes a few seconds. When I access the file over VPN on the remote network resource, it takes a hour.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.