2010-07-27 119 views
2

基本上我有一个使用Excel应用程序编写的程序。C#如何阻止用户使用程序创建的Excel应用程序

运行我的程序时,如果用户试图打开任何Excel文件,该文件在我的程序创建的Excel实例上打开。

我想这与excel打开 “Excel.exe”的现有实例有关。

当我的程序退出时,它会导致僵尸excel进程。

我想要做的是隐藏我的excel实例被用户使用。 (或任何其他解决方案的情况)

我试图谷歌的东西出来,但没有到目前为止。

我的代码看起来像这样。

foreach (string strFileName in this.m_ListRequestFileNames) 
{ 
    object misVal = System.Reflection.Missing.Value; 
    try 
    { 
     if (Common.IsOpened(strFileName) == true) 
     { 
      this.m_ParentWnd.Log(strFileName + " is in use by another program." + Environment.NewLine); 
      continue; 
     } 
     this.m_xlWBook = this.m_xlApp.Workbooks.Open(strFileName, 3, true, misVal, misVal, misVal, misVal, misVal, misVal, false, misVal, misVal, misVal, misVal, misVal); 
     if (Make(ref m_xlWBook, m_strDstPath, ref m_ListIgnoreSheetNames) != true) 
     { 
      this.m_ParentWnd.Log("-!- Error while making " + m_xlWBook.Name + " into a csv file" + Environment.NewLine); 
      continue; 
     } 
     this.m_ParentWnd.Log("Completed " + m_xlWBook.Name + Environment.NewLine); 
    } 
    catch (System.Exception e) 
    { 
     m_ParentWnd.Log(e.ToString()); 
    } 
    this.m_xlWBook.Close(false, null, null); 
} 
this.m_xlApp.Quit(); 
Common.releaseObject(this.m_xlApp); 
+1

这是一个Web应用程序运行在服务器或桌面客户端应用程序? – 2010-07-27 03:56:17

+0

这是一个客户端应用程序。 – Die4ACause 2010-07-27 04:59:57

回答

3

的问题,从我从你的代码中看到的最不重要的,是你不关闭Excel中,一旦你完成(app.Quit) - 如果你是,你可能不会配置对象你创建。使用Excel互操作,你已经没有了垃圾收集的乐趣,你必须小心地清理自己。一个被遗忘的小例子会让Excel在后台处理你所描述的恼人的副作用。看看这个StackOverflow线程进行了广泛的讨论: How do I properly clean up Excel interop objects?

+0

非常感谢!我在不使用quit();的情况下释放com对象。它解决了我的僵尸进程问题。 但是有没有办法阻止用户访问我的C#程序创建的excel实例?我会很感激。 – Die4ACause 2010-07-27 05:42:57

相关问题