简体   繁体   中英

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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