C# open file from network with specified user.

I am using these codes to open file from network with specified username of domain, but it shows error:

"The specified executable is not a valid application for this OS platform."

Please help me!


Process proc = new Process();
            proc.StartInfo = new ProcessStartInfo(@"\\10.107.11.11\Shared\ExcelTest.xls");
            proc.StartInfo.UserName = "MyUserName";
            proc.StartInfo.Domain = "MYDOMAIN";

            
            string PwString = "PassWord";
            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();

Open in new window

JameMeckAsked:
Who is Participating?
 
brutaldevCommented:
Do you have Excel installed and in the path? Are you using an Excel Viewer? You need to specify the full path of the application you want to use to open the XLS file.

The shell normally does this for you but you lose that ability when using credentials over the network. If you still get stuck then I would suggest the following:
1. Instead of opening the file from the network, just copy it to a local temporary location first (http://stackoverflow.com/questions/659013/accessing-a-shared-file-unc-from-a-remote-non-trusted-domain-with-credentials).
2. Open the file from your local temp directory making sure UseShellExecute = true.

That way the shell can figure out how to open the file.
0
 
brutaldevCommented:
You need to have ShellExecute on so that it knows how to open the XLS files (with Excel):
proc.StartInfo.UseShellExecute = true;

Open in new window

If you must have this off then you should start Excel passing in the the file from the network as an argument:
proc.StartInfo = new ProcessStartInfo("Excel.exe", @"\\10.107.11.11\Shared\ExcelTest.xls");

Open in new window

0
 
brutaldevCommented:
Since you are using Password you'll have to go for option 2 since you cannot shell execute when using this (according to the documentation), you must specify the executable to use when trying to open a specific file..
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
JameMeckAuthor Commented:
I did as you said, then here is the error:

The Process object must have the UseShellExecute property set to false in order to start a process as a user.
0
 
brutaldevCommented:
Check my second comment above :)
0
 
JameMeckAuthor Commented:
Dear brutaldev,
I did your second option, but here is the error:

 "The system cannot find the file specified"
0
 
JameMeckAuthor Commented:
Thanks! I will try your way.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.