This question already has answers here:
Process.Start() impersonation problem
(3 answers)
Impersonating in .net (C#) & opening a file via Process.start
(1 answer)
Closed 4 years ago.
I need to open documents/files with their associated application and a specific user in C#.
Problem 1: Process.Start() with Domain/Username/Password (in ProcessStartInfo) doesn't accept an (e. g.) image file anymore, it expects an executable then (e. g. cmd.exe).
Problem 2: Process.Start() with Username "doesn't find the directory" when I try to call cmd.exe.
Problem 3: When using "runas" I cannot supply the specific user's password.
Problem 4: When using "psexec" (which takes a password) Windows 10 asks me for the application to open the image with. (But it works fine with e.g. an Excel file.)
Why is "cmd" just opening the image fine with the Windows image viewer while "runas" opens the image with Paint.net and "psexec" asks for the application???
var cmdParamsA = #"c:\temp\Image.png";
Process.Start("cmd", cmdParamsA);
var cmdParamsB = #"/noprofile /user:domain\user cmd /k start c:\temp\Image.png";
Process.Start("runas", cmdParamsB);
var cmdParamsC = #"-u domain\user -p password cmd /k start c:\temp\Image.png";
Process.Start(#"c:\temp\PsExec.exe", cmdParamsC);
Related
This question already has answers here:
Updating HKEY_CURRENT_USER hive from a service
(2 answers)
Closed 4 years ago.
I have the problem with accessing "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run". Function returns result 2, that means "File not found". I'm pretty sure this happened because of Local System rights in application but don't know how to fix this.
int result = RegOpenKeyEx(registryHive, registrySubname, 0,STANDART_RIGHTS_READ | KEY_QUERY_VALUE | KEY_NOTIFY , out registryKey);
Any ideas?
UPD: I've checked this in application with current user rights and everything worked
When running as the LocalSystem account, such as in a service, you can't use RegOpenKeyEx() to open the HKEY_CURRENT_USER hive of any user account other than LocalSystem. To open the HKCU hive of another user, you need to first impersonate that user, such as with ImpersonateLoggedOnUser(), and then use RegOpenCurrentUser().
This question already has answers here:
C# Process.Start parameters truncated
(2 answers)
Closed 5 years ago.
I'm using this code for opening a browser with a url inside.Cause my app uses smtp server for senting sms,i'm creating a stringbuilder with all my mobile phones. So if my url its over e.x 2000 characters.My browser opens but my url it is not complete.On the other method,if i will get my url as string and copy-paste into my browser it works fine.
Process.Start("MyWebsite/mobiles="+richTextBox1.Text);
You should provide path of the browser by first string and arguments by the second string as it is described by MSDN MSDN
Start(String, String) Starts a process resource by specifying the
name of an application and a set of command-line arguments, and
associates the resource with a new Process component.
This question already has answers here:
Checking file/folder access permission
(3 answers)
Closed 8 years ago.
I have an Application running in a server, that takes a username and file path. The idea to check if the user can read the file (the target user is not the same user running the program).
So how to check read permissions for specific user ??
I can't take responsibility for this as I googled it and the answer was by James Newton-King found here- How to present credentials in order to open file?
You want to impersonate a user who does have the rights to access the file.
I recommend using a class like this - http://www.codeproject.com/KB/cs/zetaimpersonator.aspx. It hides all the nasty implementation of doing impersonation.
using (new Impersonator("myUsername", "myDomainname", "myPassword"))
{
string fileText = File.ReadAllText("c:\test.txt");
Console.WriteLine(fileText);
}
Check this Documentation, this might be useful:
http://msdn.microsoft.com/en-us/library/system.io.file.getattributes(v=vs.110).aspx
I have written a win application that works on a network.
In one of its forms user can browse and select file from local computer and add it to a list, so the application copies these files to a folder in My Network Places that no users can access to it but one that i have created already for my application so i have his username and password.Every things works fine up to here.
In this form user can also open a file by select it and press open button, so the file should open in an application that relates to its extension(for example test.xlsx should open in Exel.exe).I have used Process.Start() to do this but for each extension i receive an individual error(for example "Access is denied" for NotePad and "RunTime error" for AdobeReader and "Not enough memory" for Excel).
What is my mistake?
Note : I have used ImpersonatUser to logon that user in my application.
Edit : I have used following code to open the file :
Using(WindowsImpersonationContext impersonateUser = LogonMethod())
{
ProcessStartInfo pInfo = new ProcessStartInfo(filePathWithExtension);
pInfo.Domain = MyDomainName;
pInfo.UseShellExecute = false;
Process.Start(pInfo);
}
Note : My LogonMethod uses LogonUser method of advapi32.dll.
The behavior you are seeing is almost expected.
it looks like you are not launching application directly, but rater using association by file name. I don't believe you'll get application launched under account you want. You can check what account application is run unrder using task manager.
Most application are not tested to run in "run as" context, so they may work correctly or fail randomly.
I could not solve this problem, so i have used another way.I have copied the file to a temp folder and then used Process.Start to open this new file.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Launching a Application (.EXE) from C#?
I am trying to run an exe as a process from my C# code. The exe is a secure one and asks for a password before executing the command. I am unable to pass the password to the exe by any means. When i try to write a standard input it does not take it. Have anyone faced this kind of an issue. If so pls share your work arounds for the scenario. Thanks in advance
I am editing the post to make it a little clear. The exe that i am trying to run is a command line exe. Its a white listing tool provided by mcafee. What i am trying to do is to set the password to the exe so that i can make it secure. the command goes like this
sadmin passwd
once i execute the command it will ask me for the password i want to assign
new password:
once i enter the password it will ask me to re-enter password
re-enter password:
Once the password is set, subsequently when i run other commands on the sadmin, It will prompt me to enter the password
sadmin enable
password:
The password is not an argument to the command. It is passed to the prompt after running the command. So i am unable to achieve this by adding password as an argument to the command.
you can try
Process example= new Process();
example.StartInfo.FileName = "example.exe";
example.StartInfo.Arguments = "arg";
example.Start();
Have you tried using Process.StartInfo.Arguments? Something like this:
Process MyProcess = new Process();
MyProcess.StartInfo.FileName = "PathToExe";
MyProcess.StartInfo.Arguments = "YourArgsHere";
MyProcess.StartInfo.WorkingDirectory = "DesiredWorkingDir(Optional)";
MyProcess.Start();