我正在尝试使用C#创建Windows服务,该服务将从网络驱动器复制所有文件并将其粘贴到本地驱动器(例如在C驱动器中)。当我运行测试用例时,程序成功运行,但是当我安装并运行Windows服务时,日志文件中出现“访问被拒绝”错误。

我尝试了Map Network Drive (API)解决方案,但该解决方案无效。任何一个。

这是我用于从网络驱动器获取所有文件并将其粘贴到本地驱动器文件夹中的示例代码

Service1.cs

    public partial class Service1 : ServiceBase
    {
        private Timer _timer;

        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            try
            {

                DoWork();

            }
            catch (Exception e)
            {
                WriteErrorLog(e);
            }

        }

        private void DoWork()
        {
            _timer = new Timer();
            _timer.Interval = 5000;
            _timer.Enabled = true;
            _timer.Elapsed += _timer_Elapsed;
            Update();
        }

        private void Update()
        {
            RevidAddinController.Update_AutodeskAddinFolder_With_ArchcorpUpdatedAddinFiles(Configuration.AutodeskVersion, Configuration.AutodeskRevitAddinFolderPath);

        }

        private void _timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            Update();
        }

        private void WriteErrorLog(Exception ex)
        {
            StreamWriter sw = null;

            try
            {
                sw = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "\\Logfile.txt", true);
                sw.WriteLine(DateTime.Now.ToString() + " ; " + ex.Source.ToString().Trim() + "; " + ex.Message.ToString().Trim());
                sw.Flush();
                sw.Close();

            }
            catch
            {

            }
        }

        protected override void OnStop()
        {
        }

    }

RevidAddinController.cs
    public static class RevidAddinController
    {

        public static IEnumerable<AddinStatus> Update_AutodeskAddinFolder_With_ArchcorpUpdatedAddinFiles(List<string> autoDeskVersion, string addinInstallationPath)
        {
            var networkDrive = ActivateNetworkDrive();
            var allAutodeskVersionPath = Util.GetAllAutodeskAddinLibraryFolderPaths(autoDeskVersion, addinInstallationPath);

            List<FileData> latestArchcorpAddins = new List<FileData>();

            foreach (var autodeskAddinFolder in allAutodeskVersionPath)
            {
                var archorpAddinFiles = Util.GetAllExternalRevitAddinFilesFromArchcorpAddinFolderPath(Configuration.ArchcorpAddinFolderPath);
                var autodeskAddinFiles = Util.GetAllExternalRevitAddinFilesLocationFromAutodeskAddinFolderPath(autodeskAddinFolder);

                var latestAddins = Util.GetUpdatedRevitAddinFromArchcorpFolderPath(autodeskAddinFolder, archorpAddinFiles, autodeskAddinFiles)
                                       .Where(addin => !addin.FileName.Contains(Configuration.DeleteAddinNamePrefix));

                latestArchcorpAddins.AddRange(latestAddins);
            }

            List<AddinStatus> addinCopyStatus = new List<AddinStatus>();
            foreach (var autodeskAddinPath in allAutodeskVersionPath)
            {
                foreach (var newArchcorpAddin in latestArchcorpAddins)
                {
                    addinCopyStatus.Add(Util.InstallNewAddinFile(newArchcorpAddin, autodeskAddinPath));
                }
            }

            return addinCopyStatus;
        }


    /// <summary>
    /// Map the network drive path
    /// </summary>
    /// <returns></returns>
    public static NetworkDrive ActivateNetworkDrive()
    {
        NetworkDrive oNetDrive = new aejw.Network.NetworkDrive();
        try
        {
            oNetDrive.LocalDrive = "O:";
            oNetDrive.ShareName = @"\\acdxbfs1\Organisation";
            oNetDrive.Force = true;
            oNetDrive.Persistent = true;
            oNetDrive.MapDrive();
        }
        catch (Exception err)
        {
            throw err;
        }
        return oNetDrive;
    }

    }

完整的代码可以在gist here上找到。如果有人检查了代码并提供了有关此问题的任何反馈/解决方案,我们将不胜感激。

最佳答案

在默认的本地系统帐户下运行服务将没有共享的概念。这些是在用户帐户下设置的。

您的2个选项

  • 在已映射这些份额的用户帐户下运行服务
  • 通过和IP地址而不是驱动器号访问您的共享。但是,您将需要相应地设置文件/文件夹权限。
  • 关于c# - 运行Windows服务时无法访问网络驱动器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49595332/

    10-15 08:47