使用Windows服务使用cmd文件将文件复制到网络路径 [英] Copy file to network path using Windows Service using cmd file

查看:1879
本文介绍了使用Windows服务使用cmd文件将文件复制到网络路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用C#代码从本地文件夹复制xml文件到共享路径。

I am trying to copy the xml file from the local folder to shared path using C# code in my Windows Service.

它正在调用CMD文件并返回拒绝访问。但是如果我尝试复制到本地也是一样。

It is calling the CMD file and returns Access Denied. But same is works if I try copying to local.

private void CopyFile(string path)
    {
        try
        {

            Process process = new Process();
            ProcessStartInfo startInfo = new ProcessStartInfo();                
            startInfo.UseShellExecute = false;
            startInfo.FileName = Path.Combine(Environment.CurrentDirectory, "Batch", "Run.cmd");
            startInfo.Arguments = "/c " + path;                               
            startInfo.RedirectStandardError = true;
            startInfo.RedirectStandardOutput = true;

            process = Process.Start(startInfo);
            process.WaitForExit();
            string output = process.StandardOutput.ReadToEnd();
            string error = process.StandardError.ReadToEnd(); // ACCESS DENIED
            int  exitCode = process.ExitCode; // 1


            process.Close();

        }
        catch (Exception ex)
        {
            string x = ex.Message;
        }
    }

Run.cmd / p>

Run.cmd

@set sourcePath=%1

copy /y %sourcePath%\MyTest.xml \\networksharedPath\XML\MyTest.xml

Windows服务的项目安装程序已配置为使用 LocalSystem 帐户。

The Windows service's project installer has configured to use LocalSystem Account.

如何使服务将文件从本地文件夹复制到共享机器?任何与C#代码或Windows过程安装程序配置的问题?

How to make the service to copy the file from local folder to shared machine? Any issues with the C# code or Windows Process Installer configuration?

请注意:手动单击cmd文件复制到共享文件夹。如果我修改作为网络服务和运行我得到相同的错误访问被拒绝。尝试的配置步骤在 http://stackoverflow.com/a/11983513/1559213 中给出,但没有运气..

Note: Manually clicking the cmd file copies to the shared folder. If I modify as Network Service and run I get same error Access is Denied. Tried configuring steps given in http://stackoverflow.com/a/11983513/1559213 But no luck..

推荐答案

LocalSystem帐户默认情况下通常不能访问网络共享。尝试在您的帐户下运行服务。通过手动运行cmd文件,它将在您的帐户下运行,而不是在LocalSystem帐户下运行。它不会抛出任何异常,当它作为自己的进程运行。因此,您可以在这里跟踪控制台输出。

LocalSystem Account normally does not have access to network shares by default. Try to run the service under your account. By manually running the cmd file it does run under your account and not under LocalSystem account. It will not throw any exceptions while it run as own process. So possibly you can as well trace console output here.

这篇关于使用Windows服务使用cmd文件将文件复制到网络路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆