使用C#将文件从本地驱动器复制到共享驱动器 [英] Copy a file from local drive to shared drive using C#

查看:65
本文介绍了使用C#将文件从本地驱动器复制到共享驱动器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将文件从本地驱动器复制到共享网络路径.

I want to copy a file from my local drive to a shared network path.

我尝试了以下方法:

string remoteUserName = 
          WebConfigurationManager.AppSettings["remoteUsername"].ToString();
string remotePassword =
          WebConfigurationManager.AppSettings["remotePassword"].ToString();
string remoteDomain = 
          WebConfigurationManager.AppSettings["remoteDomain"].ToString();
string remoteFilePath = 
          WebConfigurationManager.AppSettings["remoteFilePath"].ToString();

using (var impersonation = new 
          ImpersonatedUser(remoteUserName, remoteDomain, remotePassword))
{
    CreateErrorLog("Logged in successfully - User and password are correct.", 
                   "Action" + " - " + "controllerName");
    string filePath = remoteFilePath;
    string fileName = "txt.txt";
    StreamWriter SW1;
    FileIOPermission myPerm = new 
               FileIOPermission(FileIOPermissionAccess.AllAccess, filePath + fileName);
    myPerm.Assert();
    SW1 = System.IO.File.CreateText(filePath + fileName);
}

推荐答案

好,让我们来研究一下这段代码.首先,让我们简化构建路径.我们有一个网络路径和一个本地路径.根据您当前的代码,网络路径是使用一些变量comboBox1,comboBox2和Environment.UserName构建的,所以让我们做些不同:

Ok, let's work on this code a little. First let's simplify building the paths. We have a network path and a local path. According to your current code the network path is built with a few variables comboBox1, comboBox2, and Environment.UserName, so let's do it a little different:

var networkPath = Path.Combine(@"\\network",
    comboBox1.SelectedItem as string,
    comboBox2.SelectedItem as string,
    Environment.UserName);

这将把\正确地放在每个字符串之间(即,如果已经有一个反斜杠,则不会添加一个,但是如果需要的话).

that's going to place the \ in between each of those strings properly (i.e. if there were already a back slash it wouldn't add one, but would if necessary).

现在让我们对本地路径执行相同操作:

Now let's do the same for the local path:

var localPath = Path.Combine(@"C:\Users",
    Environment.UserName,
    "test",
    label5.Text);

好的,我们快到了,但是我们还有一条替代的网络路径:

ok, we're almost there, but we also have an alternative network path:

var alternativeNetworkPath = Path.Combine(@"\\atlanta2-0\it-documents\filestroage",
    comboBox1.SelectedItem as string,
    comboBox2.SelectedItem as string,
    Environment.UserName,
    label5.Text);

现在,关于此路径的一件事已经让我怀疑,这是\ filestroage,实际上拼写错误.现在,如果该文件夹的拼写方式没问题,但我想知道它的拼写是否错误.因此,请看一看.好吧,让我们继续,现在我们已经构建了所有三个路径,它有点容易阅读,并且我们可以轻松地输出这些字符串以确保它们正确.让我们看一下逻辑.它说明了这一点,如果networkPath存在,则将其保存在那里,但是,如果不存在,则创建它并将其保存到AlternativeNetworkPath中.因此,让我们这样做:

now, one thing about this path that's already suspect to me is this, \filestroage, that's actually spelled wrong. Now, if the folder is spelled that way fine, but I'm wondering if it's spelled wrong. So just take a look. Alright, let's continue on, now we have all three paths built, it's a little easier to read, and we can easily output those strings to ensure they are right. Let's take a look at the logic. It says this, if the networkPath exists then save it there, however, if it does not exist then create it and save it to the alternativeNetworkPath. So let's do that:

if (Directory.Exists(networkPath))
{
    File.Copy(localPath, networkPath);
}
else
{
    Directory.CreateDirectory(networkPath);
    File.Copy(localPath, alternativeNetworkPath);
}

好吧,足够简单了吗?但是您说过Directory.Exists即使存在也返回true.这是非常令人期待的,不是吗?如果目录存在,则此方法肯定会返回true,否则将返回false.然后,您使用Directory.CreateDirectory声明,上面的行表示找不到网络名称-仅表示路径构造错误.

alright, simple enough yes? But you stated that the Directory.Exists is returning true even if it exists. That's pretty much expected isn't it? If the directory exists then this method would certainly return true, if not then it would return false. You then stated with the Directory.CreateDirectory that The line above says the network name cannot be found - that can only mean that the path was constructed wrong.

因此,将其分解后,最重要的是,正在构建的路径必须平整.但是,使用此新模型,您应该能够更轻松地拉出这些路径.因此,在我看来,整个方法如下所示:

So after breaking it down, the bottom line is this, the paths being constructed have to be off a tidge. However, with this new model you should be able to pull those paths out a lot easier. So the entire method, in my mind, would look something like this:

var networkPath = Path.Combine(@"\\network",
    comboBox1.SelectedItem as string,
    comboBox2.SelectedItem as string,
    Environment.UserName);

var localPath = Path.Combine(@"C:\Users",
    Environment.UserName,
    "test",
    label5.Text);

var alternativeNetworkPath = Path.Combine(@"\\atlanta2-0\it-documents\filestroage",
    comboBox1.SelectedItem as string,
    comboBox2.SelectedItem as string,
    Environment.UserName,
    label5.Text);

if (Directory.Exists(networkPath))
{
    File.Copy(localPath, networkPath);
}
else
{
    Directory.CreateDirectory(networkPath);

File.Copy(localPath, alternativeNetworkPath);
}

所以现在让我们看一下这些变量中的那些路径,您的问题就应该出来了.

and so now let's have a look at those paths in those variables and your problem should come right out.

这篇关于使用C#将文件从本地驱动器复制到共享驱动器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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