/////Config File////
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name ="Application" type="CustomSectionsExample.MyHandler, CustomSectionsExample"/>
</configSections>
<Application>
<Name>POWERPNT</Name>
<Runtime>1</Runtime>
<Message>Say Hey to Word application</Message>
<MessageTime>1</MessageTime>
</Application >
</configuration>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Create Custom Handler
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration.Install;
using System.Configuration;
namespace CustomSectionsExample
{
public class MySettings
{
public string Name;
public MySettings()
{
}
}
public class MyHandler: IConfigurationSectionHandler
{
public MyHandler()
{
}
#region IConfigurationSectionHandler Members
public object Create(object parent, object configContext, System.Xml.XmlNode section)
{
MySettings Settings = new MySettings();
Settings.Name = section.SelectSingleNode("Name").InnerText;
return Settings;
}
#endregion
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Finally Read from App.Config
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
namespace CustomSectionsExample
{
class Program
{
static void Main(string[] args)
{
MySettings settings = (MySettings)ConfigurationManager.GetSection("Application");
Console.WriteLine(settings.Name);
Console.ReadKey();
}
}
}
Tuesday, December 21, 2010
Run Open Office Applications
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TTA.Util;
using unoidl.com.sun.star.frame;
using unoidl.com.sun.star.lang;
using unoidl.com.sun.star.beans;
namespace TTA.Util
{
public class OpenOffice
{
XComponent xComponent;
XComponentLoader componentLoader;
unoidl.com.sun.star.lang.XMultiServiceFactory multiServiceFactory;
unoidl.com.sun.star.uno.XComponentContext localContext;
public OpenOffice(string FileName, string fileType)
{
localContext = uno.util.Bootstrap.bootstrap();
multiServiceFactory = (unoidl.com.sun.star.lang.XMultiServiceFactory)localContext.getServiceManager();
componentLoader = (XComponentLoader)multiServiceFactory.createInstance("com.sun.star.frame.Desktop");
PropertyValue[] loadDesc = new PropertyValue[1];
loadDesc[0] = new PropertyValue();
loadDesc[0].Name = "Hidden";
loadDesc[0].Value = new uno.Any(true);
xComponent = componentLoader.loadComponentFromURL(fileType, "_blank", 0, loadDesc);
((XStorable)xComponent).storeToURL(General.PathConverter(FileName), new unoidl.com.sun.star.beans.PropertyValue[0]);
xComponent.dispose();
xComponent = componentLoader.loadComponentFromURL(General.PathConverter(FileName), "_blank", 0, new unoidl.com.sun.star.beans.PropertyValue[0]);
}
public bool Close()
{
try
{
((XStorable)xComponent).store();
xComponent.dispose();
return true;
}
catch
{
return false;
}
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Now to open the application
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
private void Impress_Load(object sender, EventArgs e)
{
string FileName = General.GetAppString("UserDocuments") + GlobalData.GetStudentInfo()[0].ToString().Trim() + "_" + GlobalData.GetUserName() + General.GetFileExtension(General.GetAppString("OpenOfficeApp1"));
GlobalData.SetFileName(FileName);
op = new OpenOffice(FileName, "private:factory/" + General.GetAppName(General.GetAppString("OpenOfficeApp1")));
CountDownTimer ct = new CountDownTimer();
ct.Show();
timer1.Interval = 1000;
timer1.Enabled = true;
Process[] procs = System.Diagnostics.Process.GetProcessesByName("SOFFICE", ".");
IntPtr hWnd = procs[0].MainWindowHandle;
SwitchToThisWindow(hWnd, false);
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TTA.Util;
using unoidl.com.sun.star.frame;
using unoidl.com.sun.star.lang;
using unoidl.com.sun.star.beans;
namespace TTA.Util
{
public class OpenOffice
{
XComponent xComponent;
XComponentLoader componentLoader;
unoidl.com.sun.star.lang.XMultiServiceFactory multiServiceFactory;
unoidl.com.sun.star.uno.XComponentContext localContext;
public OpenOffice(string FileName, string fileType)
{
localContext = uno.util.Bootstrap.bootstrap();
multiServiceFactory = (unoidl.com.sun.star.lang.XMultiServiceFactory)localContext.getServiceManager();
componentLoader = (XComponentLoader)multiServiceFactory.createInstance("com.sun.star.frame.Desktop");
PropertyValue[] loadDesc = new PropertyValue[1];
loadDesc[0] = new PropertyValue();
loadDesc[0].Name = "Hidden";
loadDesc[0].Value = new uno.Any(true);
xComponent = componentLoader.loadComponentFromURL(fileType, "_blank", 0, loadDesc);
((XStorable)xComponent).storeToURL(General.PathConverter(FileName), new unoidl.com.sun.star.beans.PropertyValue[0]);
xComponent.dispose();
xComponent = componentLoader.loadComponentFromURL(General.PathConverter(FileName), "_blank", 0, new unoidl.com.sun.star.beans.PropertyValue[0]);
}
public bool Close()
{
try
{
((XStorable)xComponent).store();
xComponent.dispose();
return true;
}
catch
{
return false;
}
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Now to open the application
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
private void Impress_Load(object sender, EventArgs e)
{
string FileName = General.GetAppString("UserDocuments") + GlobalData.GetStudentInfo()[0].ToString().Trim() + "_" + GlobalData.GetUserName() + General.GetFileExtension(General.GetAppString("OpenOfficeApp1"));
GlobalData.SetFileName(FileName);
op = new OpenOffice(FileName, "private:factory/" + General.GetAppName(General.GetAppString("OpenOfficeApp1")));
CountDownTimer ct = new CountDownTimer();
ct.Show();
timer1.Interval = 1000;
timer1.Enabled = true;
Process[] procs = System.Diagnostics.Process.GetProcessesByName("SOFFICE", ".");
IntPtr hWnd = procs[0].MainWindowHandle;
SwitchToThisWindow(hWnd, false);
}
Open and run Word, Excel, PowerPoint Applications
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using Microsoft.Office.Core;
using Microsoft.Office.Interop.Excel;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using TTA.Util;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace TTA
{
public partial class MicrosoftOffice : Form
{
public static PowerPoint.Application objApp;
public static PowerPoint.Presentations objPresSet;
public static PowerPoint._Presentation objPres;
public static PowerPoint.Slides objSlides;
public static PowerPoint._Slide objSlide;
public static string ProcessName = null;
public MicrosoftOffice()
{
InitializeComponent();
}
[DllImport("user32.dll", SetLastError = true)]
static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab);
static void PowerpointWindow()
{
//Create a new presentation based on a template.
objApp = new PowerPoint.Application();
objApp.Visible = MsoTriState.msoTrue;
objPresSet = objApp.Presentations;
objPres = objPresSet.Open("D:\\Temp\\GDR.ppt", MsoTriState.msoCTrue, MsoTriState.msoTrue, MsoTriState.msoTrue);
objSlides = objPres.Slides;
//Build Slide 1:
objSlide = objSlides.Add(1, PowerPoint.PpSlideLayout.ppLayoutTitleOnly);
objPres.SaveAs("D:\\Temp\\GDR.ppt", Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsDefault, MsoTriState.msoTrue);
ProcessName = "POWERPNT";
}
public bool CloseWindow(PowerPoint.Application App)
{
return true;
}
private void timer1_Tick(object sender, EventArgs e)
{
if (GlobalData.GetMicrosoftOfficeWindowState())
{
timer1.Enabled = false;
timer1.Enabled = false;
//if (objApp.Windows.Count == 0)
//{
// MessageBox.Show("Powerpoint closed from outside.");
// System.Windows.Forms.Application.Exit();
//}
Process[] processes = System.Diagnostics.Process.GetProcessesByName(ProcessName, ".");
foreach (Process p1 in processes)
{
p1.Kill();
}
ImpressResult result = new ImpressResult();
result.MdiParent = this.MdiParent;
result.Show();
this.Close();
}
}
private void MicrosoftOffice_Load(object sender, EventArgs e)
{
//PowerpointWindow();
//OpenExcel();
word();
CountDownTimer ct = new CountDownTimer();
ct.Show();
timer1.Interval = 1000;
timer1.Enabled = true;
}
static void OpenExcel()
{
object missing = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
excelApp.Visible = true;
Microsoft.Office.Interop.Excel.Workbook newWorkbook = excelApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
string workbookPath = "D:\\Temp\\SomeWorkBook2.xlsx";
newWorkbook.SaveAs(workbookPath, missing, missing, missing, true, missing, XlSaveAsAccessMode.xlNoChange, missing, missing, missing, missing, missing);
excelApp.Quit();
excelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
excelApp.Visible = true;
Microsoft.Office.Interop.Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(workbookPath,
0, false, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
ProcessName = "EXCEL";
}
static void word()
{
Microsoft.Office.Interop.Word.ApplicationClass WordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
object fileName = @"D:\Temp\sam.doc";
object readOnly = false;
object isVisible = true;
// Here is the way to handle parameters you don't care about in .NET
object missing = System.Reflection.Missing.Value;
// Make word visible, so you can see what's happening
WordApp.Visible = true;
// Open the document that was chosen by the dialog
Microsoft.Office.Interop.Word.Document aDoc = WordApp.Documents.Open(ref fileName, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing);
// Activate the document so it shows up in front
aDoc.Activate();
ProcessName = "WINWORD";
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using Microsoft.Office.Core;
using Microsoft.Office.Interop.Excel;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using TTA.Util;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace TTA
{
public partial class MicrosoftOffice : Form
{
public static PowerPoint.Application objApp;
public static PowerPoint.Presentations objPresSet;
public static PowerPoint._Presentation objPres;
public static PowerPoint.Slides objSlides;
public static PowerPoint._Slide objSlide;
public static string ProcessName = null;
public MicrosoftOffice()
{
InitializeComponent();
}
[DllImport("user32.dll", SetLastError = true)]
static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab);
static void PowerpointWindow()
{
//Create a new presentation based on a template.
objApp = new PowerPoint.Application();
objApp.Visible = MsoTriState.msoTrue;
objPresSet = objApp.Presentations;
objPres = objPresSet.Open("D:\\Temp\\GDR.ppt", MsoTriState.msoCTrue, MsoTriState.msoTrue, MsoTriState.msoTrue);
objSlides = objPres.Slides;
//Build Slide 1:
objSlide = objSlides.Add(1, PowerPoint.PpSlideLayout.ppLayoutTitleOnly);
objPres.SaveAs("D:\\Temp\\GDR.ppt", Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsDefault, MsoTriState.msoTrue);
ProcessName = "POWERPNT";
}
public bool CloseWindow(PowerPoint.Application App)
{
return true;
}
private void timer1_Tick(object sender, EventArgs e)
{
if (GlobalData.GetMicrosoftOfficeWindowState())
{
timer1.Enabled = false;
timer1.Enabled = false;
//if (objApp.Windows.Count == 0)
//{
// MessageBox.Show("Powerpoint closed from outside.");
// System.Windows.Forms.Application.Exit();
//}
Process[] processes = System.Diagnostics.Process.GetProcessesByName(ProcessName, ".");
foreach (Process p1 in processes)
{
p1.Kill();
}
ImpressResult result = new ImpressResult();
result.MdiParent = this.MdiParent;
result.Show();
this.Close();
}
}
private void MicrosoftOffice_Load(object sender, EventArgs e)
{
//PowerpointWindow();
//OpenExcel();
word();
CountDownTimer ct = new CountDownTimer();
ct.Show();
timer1.Interval = 1000;
timer1.Enabled = true;
}
static void OpenExcel()
{
object missing = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
excelApp.Visible = true;
Microsoft.Office.Interop.Excel.Workbook newWorkbook = excelApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
string workbookPath = "D:\\Temp\\SomeWorkBook2.xlsx";
newWorkbook.SaveAs(workbookPath, missing, missing, missing, true, missing, XlSaveAsAccessMode.xlNoChange, missing, missing, missing, missing, missing);
excelApp.Quit();
excelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
excelApp.Visible = true;
Microsoft.Office.Interop.Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(workbookPath,
0, false, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
ProcessName = "EXCEL";
}
static void word()
{
Microsoft.Office.Interop.Word.ApplicationClass WordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
object fileName = @"D:\Temp\sam.doc";
object readOnly = false;
object isVisible = true;
// Here is the way to handle parameters you don't care about in .NET
object missing = System.Reflection.Missing.Value;
// Make word visible, so you can see what's happening
WordApp.Visible = true;
// Open the document that was chosen by the dialog
Microsoft.Office.Interop.Word.Document aDoc = WordApp.Documents.Open(ref fileName, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing);
// Activate the document so it shows up in front
aDoc.Activate();
ProcessName = "WINWORD";
}
}
}
Tuesday, December 7, 2010
Escape characters like "\" in a string in a sql query
strLocation = strLocation + @"%";
strLocation = strLocation.Replace(@"\", @"\\");
string query = "SELECT * FROM FileFolder where idUser = "+ UserId + " and Location like '" + strLocation +"' " ;
query += "ORDER BY IsDirectory DESC, IsDefaultDir DESC, Name ASC";
try
{
Database db = new Database();
db.Param_CreateCommand(query);
//db.Param_AddWithValue(@"idUser", UserId);
//db.Param_AddWithValue(@"Location", strLocation);
DataSet ds = db.Param_GetDataSet();
db.Cleanup();
return ds;
}
catch (Exception ex)
{
General.ExceptionOccured(ex);
}
return null;
strLocation = strLocation.Replace(@"\", @"\\");
string query = "SELECT * FROM FileFolder where idUser = "+ UserId + " and Location like '" + strLocation +"' " ;
query += "ORDER BY IsDirectory DESC, IsDefaultDir DESC, Name ASC";
try
{
Database db = new Database();
db.Param_CreateCommand(query);
//db.Param_AddWithValue(@"idUser", UserId);
//db.Param_AddWithValue(@"Location", strLocation);
DataSet ds = db.Param_GetDataSet();
db.Cleanup();
return ds;
}
catch (Exception ex)
{
General.ExceptionOccured(ex);
}
return null;
Subscribe to:
Posts (Atom)