简体   繁体   English

在需要用户名/密码的网络共享上打开文件时,如何在UNC路径中传递凭据

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

I'm kinda lost here. 我有点迷路了。 I've searched everywhere, and haven't found any solution that makes sense to me. 我到处搜索,但没有找到对我有意义的解决方案。

My issue are that i need to open a specific file on a network share, but it gives me an exception that my username or password is not correct. 我的问题是我需要打开网络共享上的特定文件,但这给我一个例外,我的用户名或密码不正确。

I've put the UNC path into a variable: 我已将UNC路径放入变量中:

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

And then this code to access and run the file in the path: 然后这段代码访问并在路径中运行文件:

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

You don't put usernames or passwords in the path. 您不要在路径中输入用户名或密码。 You want to do something like this... 你想做这样的事情...

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

*Example taken from Code Project *示例摘自Code Project

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM