How do I call a batch file from an MSI - batch-file

I have an msi installer that needs to call a few batch files to finish the install procedure. The batch file copies extra files from the installer to a few directories and then modifies permissions on several of those directories. We want to continue using the batch files because there is not a lot of time left in our development schedule. I am not using WIX.
If possible I would like to capture the output of the batch as it goes and write it to a log file.
Found bellow is the code I am using to try to run the batch file from a custom action. It opens a cmd window, runs for a while but never seems to finish. If I run the same batch files directly from the command prompt they work.
//Set the environment to the directory containing the bat files
ProcessStartInfo info = new ProcessStartInfo(batch);
info.WindowStyle = ProcessWindowStyle.Hidden;
info.UseShellExecute = false;
info.RedirectStandardError = true;
info.RedirectStandardOutput = true;
if (!string.IsNullOrEmpty(argument))
info.Arguments = argument;
Process process = new Process();
process.StartInfo = info;
process.Start();
// Capture the standard error and standard output
What am I doing wrong?

I believe you'll need to create a custom action. See this question.

Many anti-virus programs may stop execution of .BAT files during an installation process, you should really be doing this using standard Windows Installer functionality or as a C++ Custom Action

Related

Open file in the default application WinForms (.NET Core) [duplicate]

i want to ask for help with opening a file from c# app with associated app.
I tried this:
ProcessStartInfo pi = new ProcessStartInfo(file);
pi.Arguments = Path.GetFileName(file);
pi.UseShellExecute = true;
pi.WorkingDirectory = Path.GetDirectoryName(file);
pi.FileName = file;
pi.Verb = "OPEN";
Process.Start(pi);
or this:
Process.Start(file);
where string file in both examples represents full path to the file trying to open. Now, everything is working well, except the (jpg) images with ACDSee app. Irfanview associations works well, MS office documents too. After trying to open the jpg image associated with acdsee it just runs the acdsee in the notification area and does not open the file.
I discovered, that in the registry CLASSES_ROOT for *.jpg images, there is an ACDSee.JPG value as associated app, and under this key there is in the shell->Open->Command a path:
"C:\Program Files\ACD Systems\ACDSee\ACDSee.exe" /dde
and I thing that this weird /dde is the reason, why i cannot open the file. I realized that in the same reg key shell->Open there is some DDEExec key entry with value [open("%1")]
For Irfan view or other checked app there is not a ddeexec, just the normal command like
"C:\Program Files (x86)\IrfanView\i_view32.exe" "%1"
that can be run from command line after swaping the %1 for file name, but I could not run the command from acdsee entry in the command line :(
So my question is, how can I set up the ProcessStartInfo object to ensure that it will run all the files as it would be in the explorer by doubleclick, the standards and this DDEExec ones? Is there something other like DDEExec that I shoul be aware of?
thanks and sorry for my EN
UPDATE: because this question still gets upvotes, I want to clarify that accepted answer works. I only had problem with old version of ACDSee and not with the Process.Start command or with the jpg extension.
Just write
System.Diagnostics.Process.Start(#"file path");
example
System.Diagnostics.Process.Start(#"C:\foo.jpg");
System.Diagnostics.Process.Start(#"C:\foo.doc");
System.Diagnostics.Process.Start(#"C:\foo.dxf");
...
And shell will run associated program reading it from the registry, like usual double click does.
In .Net Core (as of v2.2) it should be:
new Process
{
StartInfo = new ProcessStartInfo(#"file path")
{
UseShellExecute = true
}
}.Start();
Related github issue can be found here
This is an old thread but just in case anyone comes across it like I did.
pi.FileName needs to be set to the file name (and possibly full path to file ) of the executable you want to use to open your file. The below code works for me to open a video file with VLC.
var path = files[currentIndex].fileName;
var pi = new ProcessStartInfo(path)
{
Arguments = Path.GetFileName(path),
UseShellExecute = true,
WorkingDirectory = Path.GetDirectoryName(path),
FileName = "C:\\Program Files (x86)\\VideoLAN\\VLC\\vlc.exe",
Verb = "OPEN"
};
Process.Start(pi)
Tigran's answer works but will use windows' default application to open your file, so using ProcessStartInfo may be useful if you want to open the file with an application that is not the default.

Revit Copy addin dll at shutdown

I am working on creating a revit addin and I want to have it automatically pull a copy ofthe .dll and.addin files at shutdown using a batch file. By themselves the code and the batch file routines work correctly but when I have them running with each other I get a have a sharing violation for copying the .dll file. Can anyone tell me how I can get around the sharing violation? The purpose is to deploy these two files to all users and copy the file updates to their computer when they shut down Revit.
public Result OnShutdown(UIControlledApplication application)
{
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.FileName = "S:\\Revit 2015\\Addins\\Revit Tabs\\2015_RevitTab.bat";
proc.StartInfo.WorkingDirectory = "S:\\Revit 2015\\Addins\\Revit Tabs\\";
proc.Start();
return Result.Succeeded;
}
And here is the copy syntax
xcopy "S:\Revit 2015\Addins\Revit Tabs\Revit Tabs.addin" "C:\ProgramData\Autodesk\Revit\Addins\2015" /y
xcopy "S:\Revit 2015\Addins\Revit Tabs\Revit Tabs\bin\Debug\Revit Tabs.dll" "C:\ProgramData\Autodesk\Revit\Addins\2015" /y
You could add a call to your own stand-alone utility exe that monitors whether the current Revit process is still alive, and thenexecutes the add-in DLL copy process once Revit really is gone.
I wanted to same auto-update process and after a bit of trial and error I found some code that worked for me. Hopefully, you can use it or improve it.
I have ribbon.addin, ribbon.dll ("Ribbon") and commands.dll ("Commands") files. All files are installed as part of the deployment into the "%appdata%\Autodesk\Revit\Addins\2016" folder ("Local"). It's important that these are installed in the "%appdata%" folder and not the "%programdata%\Autodesk\Revit\Addins\2016" folder because of write protection issues!
The Ribbon addin is only for checking which version of the Commands is currently in the Local folder and if that's out-of-date from the Commands file I have in a shared network folder ("Shared"). Because of security, I can't read the AssemblyVersion of the Local DLL or the Shared DLL. To get around this I have a TXT file in the Local folder that has the AssemblyVersion as the first line and, in the Shared folder I have another TXT file (where I actually have the "About" information of the Commands addin) which has the Shared Commands AssemblyVersion as the first line.
So my Ribbon OnStartup(UIControlledApplication a) code checks the TXT files using System.IO.StreamReader. If the Local file is out-of-date it updates the Local TXT and DLL files with this c#:
try
{
string AddinsDir = a.ControlledApplication.CurrentUserAddinsLocation + #"\";
string tempDir = System.IO.Path.GetTempPath();
StreamWriter myStream = new StreamWriter(tempDir + "Commands.txt", false, System.Text.Encoding.Default);
myStream.WriteLine(AssemblyVersion);
//AssemblyVersion is the first line of the Shared Commands TXT file we read
myStream.Close();
File.Copy(tempDir + "Commands.txt", AddinsDir + "Commands.txt", true);
File.Delete(tempDir + "Commands.txt");
File.Delete(AddinsDir + "Commands.dll");
File.Copy(SharedPath + "Commands.dll", AddinsDir + "Commands.dll", true);
//SharedPath is the Shared folder
}
catch (Exception e)
{
TaskDialog.Show("Error Loading Ribbon", "There was an error loading the Ribbon. Please contact the BIM Manager for assistance.\n\n" + e.Message);
return Result.Failed;
}
If, at this point the code is still running the file is up-to-date and it's time to load it:
Assembly Commands = Assembly.LoadFrom(AddinsDir + "Commands.dll");
Type type = Commands.GetType("Commands.App");
//Commands.App is my class where my Ribbon is created and Events are registered
object instanceOfCommands = Activator.CreateInstance(type, new object[] { a });
return Result.Succeeded;
My plan for Revit 2017 deployment is to create my custom Ribbon in the Ribbon.dll so I can have my "About" button there and accessible at all times. Then, I'll add a button in the "About" dialog box that would manually update the Local Commands DLL.
I hope that helps!!

MStest fails when called as a batch file from Coded ui

I have a batch file which is used to run mstest.Clicking on the batch file executes the file just fine. However when the same file is called from a win form application, mstest fails. This behaviour seems quirky.Could anyone provide any reasons for this.
I have used the following code to call it :
Process myProcess = new Process();
ProcessStartInfo myProcessStartInfo = new ProcessStartInfo(#"D:\CodedUI\CommonAutomationFramework\Driver_batch.bat");
myProcessStartInfo.UseShellExecute = false;
myProcess.StartInfo = myProcessStartInfo;
myProcess.Start();
What exactly is failing? The test(s) or MSTest itself?
Anyway a batch file executes commands on the command line interpreter (cmd).
In the process you're starting, maybe you should start 'cmd.exe' instead. Read the contents of the batch file and pass them a an addiction to 'cmd.exe'
Like this:
ProcessStartInfo processInfo;
Process process;
processInfo = new ProcessStartInfo("cmd.exe", "/c " + command);
(...)
Where your 'command' here is the text in the batch file.
You could also try this:
System.Diagnostics.Process.Start(#"D:\CodedUI\CommonAutomationFramework\Driver_batch.bat");

Batch Files - Closing Opened Text Files

I currently have a Windows batch file that runs an .exe file that uses a text file. I am trying to have the Windows batch file run the .exe file multiple times. This, however, requires the use of the same text files to read from. The command prompt gives me the error that the ".txt could not be opened" (I assume from this that it is already open.)
I am trying to see if there is a way in a .bat file to system call to kill that specific text file. The suggestions I see online are to use 'taskkill notepad.exe', but that returns "invalid argument" because the program doesn't open Notepad to use the text file.
Any suggestions would be appreciated.
It sounds like your existing script fails because the first instance of the exe is still open when the second instance starts.
One thing worth trying (and this depends on the nature of the application you are invoking) is to start the executable using the START /WAIT /B ... command. This makes the command interpreter wait for the program to exit before it moves onto the next command, so as long as nothing else is locking the text files you should be OK to move onto the next command.

making a batch file run hidden/minimized without modifying the shortcut

I'm writing an application in VB.net that creates and calls batch files. I'd like these batch files to run hidden, but since there will be no shortcuts for the files, I would need to set this in the batch code itself. How would I do this?
the vbs script in the link looks good, but if you are calling the batch files from the VB app then you can run the batch files hidden:
Dim p As New Process()
p.StartInfo.FileName = "cmd.exe"
p.StartInfo.Arguments = "/C mybatchfile.bat"
p.StartInfo.CreateNoWindow = True
p.StartInfo.UseShellExecute = False
p.Start()
p.WaitForExit();// this line waits for the batch to finish, remove if you want to start the batch and continue your app while it runs.
Martyn

Resources