簡體   English   中英

使用 C# 將文件從本地驅動器復制到共享驅動器

[英]Copy a file from local drive to shared drive using C#

我想將文件從本地驅動器復制到共享網絡路徑。

我嘗試了以下方法:

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);
}

好的,讓我們稍微處理一下這段代碼。 首先讓我們簡化構建路徑。 我們有一個網絡路徑和一個本地路徑。 根據您當前的代碼,網絡路徑是用幾個變量組合框1、組合框2和環境.用戶名構建的,所以讓我們做一些不同的:

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);

現在,我已經懷疑這條路徑的一件事是,\\filestroage,實際上拼寫錯誤。 現在,如果該文件夾的拼寫正確,但我想知道它是否拼寫錯誤。 所以看看吧。 好的,讓我們繼續,現在我們已經構建了所有三個路徑,它更容易閱讀,我們可以輕松地輸出這些字符串以確保它們是正確的。 讓我們來看看邏輯。 它說的是,如果 networkPath 存在,則將其保存在那里,但是,如果它不存在,則創建它並將其保存到替代網絡路徑。 所以讓我們這樣做:

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

好吧,夠簡單了吧? 但是你說 Directory.Exists 即使存在也返回真。 這是非常預期的不是嗎? 如果目錄存在,那么這個方法肯定會返回 true,否則它會返回 false。 然后你用 Directory.CreateDirectory 聲明上面的行說找不到網絡名稱 - 這只能意味着路徑構造錯誤。

所以在分解它之后,底線是這樣的,正在構建的路徑必須遠離潮汐。 但是,使用這個新模型,您應該能夠更輕松地拉出這些路徑。 所以在我看來,整個方法看起來像這樣:

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);
}

所以現在讓我們看看這些變量中的那些路徑,你的問題應該馬上就出來了。

網絡路徑由完整的通用命名約定-UNC \\\\Server\\Share\\drive\\file路徑訪問。 如果您擁有這些類型的憑據或訪問網絡的權限,您可以使用File.Copy方法來移動您的文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM