Open file from a network folder

Asked

Viewed 421 times

3

Hello! I need to open a certain file located in a folder sharing done through Samba Server on Ubuntu Webmin. The code below seems functional, but I get the error code: Incorrect user or password.

Process proc = new Process();
        proc.StartInfo = new ProcessStartInfo("\\\\192.168.10.116\\MarcaBus\\Arquivos\\Comprovantes\\arquivo.pdf");
        proc.StartInfo.UserName = "root";
        proc.StartInfo.Domain = "MYDOMAIN";

        string PwString = "Pmjm2018!@#";
        char[] PasswordChars = PwString.ToCharArray();
        SecureString Password = new SecureString();
        foreach (char c in PasswordChars)
            Password.AppendChar(c);

        proc.StartInfo.Password = Password;
        proc.StartInfo.UseShellExecute = false;
        proc.Start();

Código de erro

How do I get around this situation?

  • Set a limit for the password in the char[10] statement. It can have a space character, or something summing up to zero without being null. Use Passwordchars.Length to count how many characters you have to make sure the password doesn’t have something else you haven’t set.

  • Instead of using you can use @ before path. Like this: Processstartinfo(@" 192.168.10.116...). @ is used to ignore possible escape characters.

  • Something about using Startinfo: https://docs.microsoft.com/pt-br/dotnet/api/system.diagnostics.process.startinfo?view=netframework-4.7.2

1 answer

3


The problem may be in the way the password is being built in the SecureString.

Try creating the following method (do not forget the using System.Security):

using System.Security;

public static SecureString ConvertToSecureString(this string password)
{
    if (password == null)
        throw new ArgumentNullException("password");

    unsafe
    {
        fixed (char* passwordChars = password)
        {
            var securePassword = new SecureString(passwordChars, password.Length);
            securePassword.MakeReadOnly();
            return securePassword;
        }
    }
}

Then use it in the encoding of password when trying to execute the file opening/process with secure data:

string PwString = "Pmjm2018!@#";
SecureString secString = PwString.ConvertToSecureString();

var processInfo = new ProcessStartInfo
{
    WorkingDirectory = @"\\192.168.10.116\MarcaBus\Arquivos\Comprovantes",
    FileName = "arquivo.pdf",
    UserName = "root", 
    Password = secString,
    Domain = "MYDOMAIN",
    UseShellExecute = false,
};

Process.Start(processInfo);

More information and details on the question Soen: Starting a process with a user name and password

  • Still presenting the same error

  • Then it is best to withdraw the answer as accepted, because it does not solve your problem :/. However, confirm that the access information is correct.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.