Expand|Select|Wrap|Line Numbers
- string savepath;
- bool foundPID;
- int ourPID = 0;
- int tmpX = 0;
- int indexRow = 1;
- int indexCol = 1;
- int[] existingPIDs;
- existingPIDs = new int[100];
- // get existing excel processes
- Process[] existingExcels = Process.GetProcessesByName("excel");
- savepath = System.Windows.Forms.Application.ExecutablePath.ToString();
- savepath = savepath.Substring(0, savepath.LastIndexOf("\\"));
- savepath = savepath + "\\Excel-Really-Sucks-Badly.xls";
- //Excel Application Object
- Excel.Application oExcelApp;
- Excel.Workbook oExcelWorkbook;
- Excel.Worksheet oExcelWorksheet;
- // build array of already existing excel process IDs
- foreach (Process proc in existingExcels)
- {
- existingPIDs[tmpX++] = proc.Id;
- }
- // create our new excel app
- oExcelApp = new Excel.Application();
- // build up the list again, this time it should include our new excel app
- existingExcels = Process.GetProcessesByName("excel");
- // find out which of the excel process IDs is our new app
- foreach (Process proc in existingExcels)
- {
- foundPID = false;
- foreach (int p in existingPIDs)
- {
- if (p == proc.Id)
- {
- foundPID = true;
- }
- }
- if(!foundPID){
- ourPID = proc.Id;
- }
- }
- oExcelApp.DisplayAlerts = false;
- oExcelApp.Visible = false;
- oExcelWorkbook = oExcelApp.Workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
- oExcelWorksheet = (Excel.Worksheet)oExcelWorkbook.Sheets["Sheet1"];
- oExcelWorksheet.Name = "fakehitswizard.com";
- // write excel worksheet header
- oExcelWorksheet.Cells[indexRow, indexCol] = "I hate Excel";
- indexRow++;
- // save excel
- oExcelWorkbook.SaveAs(savepath, Excel.XlFileFormat.xlWorkbookNormal, "", "", true, false, Excel.XlSaveAsAccessMode.xlNoChange, false, false, false, false, false);
- // close the workbook
- oExcelWorkbook.Close(true, savepath, System.Reflection.Missing.Value );
- // quit excel
- oExcelApp.Quit();
- // Unfortunately the above alone should work but it doesn't because microsoft is weak sauce
- Process[] localByName = Process.GetProcessesByName("excel");
- // user didnt have any excels open, kill excel
- if (tmpX == 0)
- {
- foreach (Process proc in localByName)
- {
- proc.Kill();
- }
- }
- // user does have excel(s) already open, only kill our apps excel
- else if (tmpX > 0 && ourPID != 0)
- {
- foreach (Process proc in localByName)
- {
- if (proc.Id == ourPID)
- {
- proc.Kill();
- }
- }
- }
try it out, open task manager, start up 0,1,2,3,4,5 or 99 excels, then run your C# app, notice task manager only starts and closes your app's excel, none of the others. Fake Hits Wizard