我使用下面的代码检查网络驱动器上是否存在目录.如果是的话我会复制文件; 如果不是我创建目录,然后将其复制.但是我遇到了问题.任何帮助将不胜感激.主要问题是IO异常,其中包含无法找到网络名称的消息.另外我的savelocation变量看起来如果斜杠没有被转义.
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);
}
Run Code Online (Sandbox Code Playgroud)
Mik*_*oud 13
好吧,让我们稍微研究一下这段代码吧.首先让我们简化构建路径.我们有一个网络路径和一个本地路径.根据你当前的代码的网络路径是建立与几个变量comboBox1,comboBox2以及Environment.UserName,让我们做一点点不同:
var networkPath = Path.Combine(@"\\network",
comboBox1.SelectedItem as string,
comboBox2.SelectedItem as string,
Environment.UserName);
Run Code Online (Sandbox Code Playgroud)
这将正确地放置\在每个字符串之间(即,如果已经有一个反斜杠,它将不会添加一个,但如果有必要的话).
现在让我们为本地路径做同样的事情:
var localPath = Path.Combine(@"C:\Users",
Environment.UserName,
"test",
label5.Text);
Run Code Online (Sandbox Code Playgroud)
好吧,我们差不多了,但我们还有另一种网络路径:
var alternativeNetworkPath = Path.Combine(@"\\atlanta2-0\it-documents\filestroage",
comboBox1.SelectedItem as string,
comboBox2.SelectedItem as string,
Environment.UserName,
label5.Text);
Run Code Online (Sandbox Code Playgroud)
现在,关于这条已经让我怀疑的道路的一件事是\filestroage,这实际上是错误的.现在,如果文件夹拼写那么好,但我想知道它是否拼写错误.所以,看看吧.好吧,让我们继续,现在我们已经构建了所有三个路径,它更容易阅读,我们可以轻松输出这些字符串以确保它们是正确的.我们来看看逻辑.它说,如果networkPath存在然后保存在那里,但是,如果它不存在然后创建它并将其保存到alternativeNetworkPath.所以我们这样做:
if (Directory.Exists(networkPath))
{
File.Copy(localPath, networkPath);
}
else
{
Directory.CreateDirectory(networkPath);
File.Copy(localPath, alternativeNetworkPath);
}
Run Code Online (Sandbox Code Playgroud)
好吧,简单就是吗?但你说这Directory.Exists是真的even if it exists.这是非常期待的不是吗?如果目录存在,那么这个方法肯定会返回true,如果没有,那么它将返回false.然后,用规定Directory.CreateDirectory的是The line above says the network name cannot be found- 这只能意味着路径构建错误的.
因此,在分解之后,底线就是这样,正在构建的路径必须脱离tidge.但是,使用这个新模型,您应该能够轻松地将这些路径拉出来.所以在我看来,整个方法看起来像这样:
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);
}
Run Code Online (Sandbox Code Playgroud)
现在让我们来看看那些变量中的那些路径,你的问题应该出来了.