从网络驱动器C#连接/复印 [英] Connecting / Copying from network drive C#

查看:100
本文介绍了从网络驱动器C#连接/复印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不完全知道如何处理这一点。我研究了一下,但我想出做空。试图连接到工作网络驱动器和(更新项目)复制出来的最新的文件夹对我来说,DIR开始为\但是当我添加一个字符串变量,将无法连接,并不会显示时我试图去检查一下。有没有这一个过程?

Not totally sure how to approach this. I've researched a bit but I've come up short. Trying to connect to the network drives at work and copy out the newest folder (updates to a project) For me, the dir starts as \ but when i add that to a string variable it won't connect and won't display when i attempt to check it. is there a process to this?

这就是我。而且它必须是错误的某种方式。

This is what i have. And it has to be wrong in someway.

string updir = @"\\NetworkDrive\updates\xxxxx";

public void CopyAll(DirectoryInfo source, DirectoryInfo target)
    {

        try
        {
            //check if the target directory exists
            if (Directory.Exists(target.FullName) == false)
            {
                Directory.CreateDirectory(target.FullName);
            }

            //copy all the files into the new directory

            foreach (FileInfo fi in source.GetFiles())
            {
                fi.CopyTo(Path.Combine(target.ToString(), fi.Name), true);
            }


            //copy all the sub directories using recursion

            foreach (DirectoryInfo diSourceDir in source.GetDirectories())
            {
                DirectoryInfo nextTargetDir = target.CreateSubdirectory(diSourceDir.Name);
                CopyAll(diSourceDir, nextTargetDir);
            }
            //success here
            copyall = true;    
        }

        catch (IOException ie)
        {
            //handle it here
            copyall = false;
        }
    }



我一直使用的复制。而且效果很好。

i've been using that to copy. And it works well.

DateTime lastHigh = new DateTime(1900, 1, 1);
        string highDir;
        foreach (string subdir in Directory.GetDirectories(updir))
        {
            DirectoryInfo fi1 = new DirectoryInfo(subdir);
            DateTime created = fi1.LastWriteTime;

            if (created > lastHigh)
            {
                highDir = subdir;
                lastHigh = created;
            }
        }

和,要找到最新的文件夹中。

and that to find the newest folder.

推荐答案

您可以用这样的尝试(指定网络共享访问权限):

You can try with something like this (specifying access rights for the network share ):

string updir = @"\\NetworkDrive\updates\somefile";

AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
WindowsIdentity identity = new WindowsIdentity(username, password);
WindowsImpersonationContext context = identity.Impersonate();

File.Copy(updir, @"C:\somefile", true);

这篇关于从网络驱动器C#连接/复印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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