my company is using Sharepoint and ADFS. For the use of WebDav however we need the users to get some tokens which they only get by opening the Internet Explorer and navigate to two sites. However they will lose the token every ~30 Minutes, so it has to be a recurring task.
So now my job is to:
Open 2 Websites with IE
Every 30 Minutes
Don't annoy the user
My current solution is "kinda" working but I am not really satisfied with it.
I have only VSExpress so no Services.
I have a minimized max opacity visible false Windows Form.
I have a GPO which copies an EXE file to the computer and then creates a timed job that starts it every 30 minutes after login. However it is not really working out, people still have trouble accessing webdav if they don't run the EXE manually. Also whenever the EXE is running the current application the user is working in loses focus which is kinda annoying when you are typing something and have to click back in.
My current code is looking like this:
private void Form1_Load(object sender, EventArgs e)
{
MainMethod();
}
private void MainMethod()
{
RegistryKey root = Registry.LocalMachine.OpenSubKey(#"SOFTWARE\Classes\InternetExplorer.ApplicationMedium\CLSID", false);
if (root!=null)
{
opensite();
Application.Exit();
}
}
private void opensite()
{
try
{
SHDocVw.InternetExplorer _ie1 = (SHDocVw.InternetExplorer)Activator.CreateInstance(Type.GetTypeFromProgID("InternetExplorer.ApplicationMedium"));
SHDocVw.InternetExplorer _ie2 = (SHDocVw.InternetExplorer)Activator.CreateInstance(Type.GetTypeFromProgID("InternetExplorer.ApplicationMedium"));
_ie1.Visible = false;
_ie2.Visible = false;
_ie1.Navigate("SITENAME1.html");
_ie2.Navigate("SITENAME2.html");
System.Threading.Thread.Sleep(10000);
_ie1.Quit();
_ie2.Quit();
}
catch(Exception e)
{
}
}
However, I feel there is a much more elegant way to do this. I heard the only way to open a hidden IE is via
(SHDocVw.InternetExplorer)Activator.CreateInstance(Type.GetTypeFromProgID("InternetExplorer.ApplicationMedium"));
But with this I rely on the registry key which not all clients have.
Can you help me open the IE in a reliable way and maybe have some tipps on how I should set the recurring task to just start every 30 minutes (because I think it is not doing it correctly atm).
Thank you all in advance.
EDIT:
Thanks to https://stackoverflow.com/users/5065008/daniel-waghorn
I now replaced the opensite bit with:
private void Form1_Load(object sender, EventArgs e)
{
MainMethod();
}
private void MainMethod()
{
openProc("SITE1.html");
openProc("SITE2.html");
Application.Exit();
}
private void openProc(string site)
{
ProcessStartInfo startInfo = new ProcessStartInfo();
string ProgramFiles = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
startInfo.FileName = ProgramFiles + #"\Internet Explorer\iexplore.exe";
startInfo.Arguments = "" + site + "";
startInfo.CreateNoWindow = true;
startInfo.ErrorDialog = false;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
Process.Start(startInfo);
}
Thanks again!
You can use ProcessStartInfo to create a new instance of IE:
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = ""C:\Program Files\Internet Explorer\iexplore.exe"";
startInfo.Arguments = "" + url + "";
startInfo.CreateNoWindow = true;
startInfo.ErrorDialog = false;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
Process.Start(startInfo);
You could use Environment.SpecialFolder.ProgramFiles to get the user's Program Files directory path if you don't want to hard-code it.
I must point out that startInfo.WindowStyle will start Internet Explorer hidden although if at any point IE decides to alter that value for any reason it may show.
Ideally if you aren't tied to using Internet Explorer to get the tokens another alternative would be to use the above code but target cURL or something similar. With this it will run in the command line which you can guarantee not to show or steal focus with startInfo.CreateNoWindow.
I think you'll find your answer in one of these links:
Handle IE To filling a form c#
Opening a Hidden Internet Explorer Window without it getting Focus?
Related
Im making an application which needs to monitor the filesystem using FileSystemWatcher, to detect how an installation affects the filesystem.
To get rid of noise i want to filter the events that are created by their creating user, and that code is working with the //BUILTIN //Administrator user, which is used by default when doing an installation. But still there are quite a bit of noise. Then i got the idea of creating a specific user that i can use for running the installation file, and filter on that specific user, and thereby getting rid of allmost all the noise.
this is my code for the process creation and start
private void executeInnoInstaller(string path, string fileName)
{
// Use ProcessStartInfo class
ProcessStartInfo installerProces = new ProcessStartInfo();
installerProces.CreateNoWindow = true;
installerProces.UseShellExecute = false;
installerProces.FileName = path + "\"" + fileName + "\"";
installerProces.WindowStyle = ProcessWindowStyle.Normal;
installerProces.UserName = "test";
System.Security.SecureString encPassword = new System.Security.SecureString();
foreach (System.Char c in "test")
{
encPassword.AppendChar(c);
}
encPassword.MakeReadOnly();
installerProces.Password = encPassword;
try
{
// Start the process with the info we specified.
// Call WaitForExit and then the using statement will close.
using (Process exeProcess = Process.Start(installerProces))
{
exeProcess.WaitForExit();
//int exitCode = exeProcess.ExitCode;
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
this code exits with a access denied.
OS=Windows
Ive already tried to run the installer.exe from the OS filehandler with SHIFT - Rightclick using the specified user, and it works.
VisualStudio is run as administrator.
Ive tried to run the build project exe file as administrator, but it does not work.
Without the user credentials, the code works and uses the //BUILTIN //Administrator account
Does anybody have any idea ?
Thank you beforehand for your time and effort.
This code works if i turn down the UAC securitylevel to the lowest.
I need to spawn processes from my C# program, but in some cases full path to an executable can be more then 256 characters.
I have studied several related topics on this site, as well as this article #MSDN. According to that information this should be possible by using \\?\ prefix, but I still couldn't make it work. It looks like system tries to start the process, but fails. I am getting "SmartScreen has stopped working" message instead.
Am I missing something? Here is my code:
private void button2_Click(object sender, EventArgs e)
{
ProcessStartInfo start = new ProcessStartInfo();
start.Arguments = "";
start.FileName = #"\\?\c:\11111111111111111111111111111111111111111111\222222222222222222222222222222222222222222222\3333333333333333333333333333333333333333333333\444444444444444444444444444444444444444444444\5555555555555555555555555555555555555555555555\6666666666666666666666666666666666666666666666\test.exe";
start.WindowStyle = ProcessWindowStyle.Normal;
start.CreateNoWindow = true;
int exitCode;
using (Process proc = Process.Start(start))
{
proc.WaitForExit();
exitCode = proc.ExitCode;
MessageBox.Show(String.Format("Exit code: {0}", exitCode));
}
}
I am running this on Windows 10, version 1703 (OS Build 15063.1387).
I am trying to start excel with a file. It works fine when I run it with the same user. But with different user, only excel starts and that also with unknown error.
private void button1_Click(object sender, EventArgs e)
{
SecureString securePwd = new SecureString();
string password = "P#ssw0rd1";
SecureString sec_pass = new SecureString();
Array.ForEach(password.ToArray(), sec_pass.AppendChar);
sec_pass.MakeReadOnly();
ProcessStartInfo ps = new ProcessStartInfo();
ps.FileName = "c:\\Program Files\\Microsoft Office\\Office15\\EXCEL.EXE";
ps.Arguments = "c:\\test_folder\\test.xlsx";
ps.WorkingDirectory = "c:\\test_folder";
ps.Domain = "test.local";
ps.UserName = "testuser";
ps.Password = sec_pass;
ps.UseShellExecute = false;
Process.Start(ps);
}
The very same code works perfectly fine by changing the process from
ps.FileName = "c:\Program Files\Microsoft Office\Office15\EXCEL.EXE";
to
ps.FileName = "notepad.EXE";
had there be any rights issue even notepad.exe also should not work.
Knowing that it's an old post, still leaving a reply for someone like me with a similar problem.
Looking at your code, it seems that you may need to set the "ps.UseShellExecute" as "true". I tried a similar code (given below) with a button in my WPF app, and it opens the excel file without issues.
private void Button_Click(object sender, RoutedEventArgs e)
{
string myPath = #"C:\Users\Lenovo\Documents\MyFile.xlsx";
ProcessStartInfo ps = new ProcessStartInfo();
ps.FileName = "excel"; // "EXCEL.EXE" also works
ps.Arguments = myPath;
ps.UseShellExecute = true;
Process.Start(ps);
}
And of course, don't forget to add the following line at the top of your .cs script.
using System.Diagnostics;
Well then, Happy coding :)
There doesn't seem to be any problem withe the code. As without changing a bit, it just started working fine again. Simply nothing. This just opens up a question like what the problem was.?
Any suggestions.?
This is very normal thing. if you working for example in company and you open share Excel file with your friend one of you will get information "File is open by another user" you can resolve this situation copy this file to for example C:/Temp and later replace it on share space.
So I am adding a help manual to my form and would like it to open a pdf in a separate window. Right now I am using this
private void helpToolStripMenuItem_Click(object sender, EventArgs e)
{
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
process.StartInfo = startInfo;
startInfo.FileName = #"Filepath";
process.Start();
}
But it obviously requires the full filepath. I want to include the pdf with the finished product and thus I won't know the full filepath on their system. I'm not sure if I can add this as a resource somehow? Sorry if I'm overlooking something simple.
I am developing a Windows Forms application in which I need to edit certain config files. Now when the user clicks on the edit option, I want to launch these config files in a simple notepad editor. Once launched I want to stall my application. Only when the user closes the notepad editor, I want to un-stall my application. How can this be done ?
I have seen these questions, but the answers have many issues. (I read the comments given there.)
Q1Q2
You can use the Exited-Event:
try
{
Process myProcess = new Process();
myProcess.StartInfo.FileName = "notepad.exe";
myProcess.StartInfo.Arguments = #"C:\PathToYourFile";
myProcess.EnableRaisingEvents = true;
myProcess.Exited += new EventHandler(myProcess_Exited);
myProcess.Start();
}
catch (Exception ex)
{
//Handle ERROR
return;
}
// Handle Exited event and display process information.
private void myProcess_Exited(object sender, System.EventArgs e)
{
eventHandled = true;
Console.WriteLine("Exit time: {0}\r\n" +
"Exit code: {1}\r\nElapsed time: {2}", myProcess.ExitTime, myProcess.ExitCode, elapsedTime);
}
var process = System.Diagnostics.Process.Start("yourfile.txt");
process.WaitForExit();
This will open your file. However, sometimes the process will be null and you will not be able to wait for exit. Why?
Process.Start Method
Return Value Type: System.Diagnostics.Process
A new Process component
that is associated with the process resource, or null if no process
resource is started (for example, if an existing process is reused).
http://msdn.microsoft.com/en-us/library/53ezey2s.aspx
You can initiate a Process and WaitForExit:
Process pr = Process.Start("Notepad");
pr.WaitForExit();