将本地文件复制到网络共享驱动器问题 [英] Copying local file to network shared drive issues

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

问题描述

我正在使用下面的代码来检查网络驱动器上的目录是否存在.如果是,我复制文件;如果没有,我创建目录,然后将其复制过来.但是我有问题.任何帮助将不胜感激谢谢.主要问题是 IO 异常,并显示无法找到网络名称的消息.如果斜线没有被转义,我的 savelocation 变量也看起来像.

I am using the code below to check if a directory on a network drive exists. If it does I copy the file; if not I create the directory and then copy it over. However I am having issues. Any help would be appreciated thanks. The main issues is an IO exception with the message the network name cannot be found. Also my savelocation variable looks like if the slashes are not being escaped.

string savelocation = @"\\network\" + comboBox1.SelectedItem + "\\" +
    comboBox2.SelectedItem+"\\"+Environment.UserName;

// When I check what savelocation is, it returns the unescaped string
// for example \\\\network\\ and so on

if (Directory.Exists(savelocation)) // this returns true even if it exists
{
    File.Copy(@"C:\Users\" + Environment.UserName + @"\test\" + label5.Text,
        savelocation + "\\" + label5.Text);
}
else {
    DirectoryInfo d = Directory.CreateDirectory(savelocation);
    // The line above says the network name cannot be found

    File.Copy(@"C:\Users\" + Environment.UserName + @"\test\" + label5.Text,
        "\\\\atlanta2-0\\it-documents\\filestroage\\" + comboBox1.SelectedItem +
        "\\" + comboBox2.SelectedItem + "\\" + Environment.UserName + label5.Text);
}

推荐答案

好的,让我们稍微处理一下这段代码.首先让我们简化构建路径.我们有一个网络路径一个本地路径.根据你当前的代码,网络路径是用几个变量comboBox1comboBox2Environment.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.

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

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