簡體   English   中英

在需要用戶名/密碼的網絡共享上打開文件時,如何在UNC路徑中傳遞憑據

[英]How to pass credentials in UNC path when opening file on a networkshare which requires username/password

我有點迷路了。 我到處搜索,但沒有找到對我有意義的解決方案。

我的問題是我需要打開網絡共享上的特定文件,但這給我一個例外,我的用戶名或密碼不正確。

我已將UNC路徑放入變量中:

protected internal static string demoPath846 = @"\\10.90.1.73\XMODemoer\XMO846";

然后這段代碼訪問並在路徑中運行文件:

File.WriteAllText(demoPath846 + @"\xmo.ini", "[system]\r\nInstanceid=846\r\nconnectionstring=" + constr846 + "\r\n[environment]\r\nA6_DRV_EDI=" + demoPath846 + "\\edi\\ \r\nA6_DRV_USER=" + demoPath846 + "\r\n\r\n[update]\r\nurl=http://10.10.62.104/xmoads/1.0 \r\ntimeout=86400000");
        string startfile846 = demoPath846 + @"\xmo.exe"; 

        Process p = new Process();
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.WorkingDirectory = demoPath846;
        p.StartInfo.FileName = startfile846;
        p.Start();

您不要在路徑中輸入用戶名或密碼。 你想做這樣的事情...

using (UNCAccessWithCredentials unc = new UNCAccessWithCredentials())
{
    if (unc.NetUseWithCredentials(uncpath, user, domain, password))
    {
        //Access your file here...
    }
    else
    {
        // The connection has failed. Use the LastError to get the system error code
        MessageBox.Show("Failed to connect to " + tbUNCPath.Text + 
                        "\r\nLastError = " + unc.LastError.ToString(),
                        "Failed to connect",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Error);
    }
}

*示例摘自Code Project

暫無
暫無

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

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