建立c#類庫項目dll
實現(xiàn)代碼:
using system;
namespace dll
{
 /// <summary>
 /// test 的摘要說明。
 /// </summary>
 [system.serializable]
 public class test
 {
  public test()
  {
   //
   // todo: 在此處添加構(gòu)造函數(shù)邏輯
   //
  }
  public string time
  {
   get
   {
    return datetime.now.tostring();
   }
  }
 }
}
using system;
using system.io;
namespace dll
{
 /// <summary>
 /// api 的摘要說明。
 /// </summary>
 public class api : system.marshalbyrefobject
 {
  public api()
  {
   //
   // todo: 在此處添加構(gòu)造函數(shù)邏輯
   //
  }
  public string gettime()
  {
   return new test().time;
  }
  public string[] getdirectories(string path)
  {
   return directory.getdirectories(path);
  }
  public string[] getfiles(string path)
  {
   return directory.getfiles(path);
  }
 }
}
添加c#windows應(yīng)用程序項目server
實現(xiàn)代碼:
using system;
using system.drawing;
using system.collections;
using system.componentmodel;
using system.windows.forms;
using system.data;
using system.runtime.remoting;
using system.runtime.remoting.channels;
using system.runtime.remoting.channels.tcp;
using dll;
namespace server
{
 /// <summary>
 /// form1 的摘要說明。
 /// </summary>
 public class form1 : system.windows.forms.form
 {
  /// <summary>
  /// 必需的設(shè)計器變量。
  /// </summary>
  private system.componentmodel.container components = null;
  public form1()
  {
   //
   // windows 窗體設(shè)計器支持所必需的
   //
   initializecomponent();
   //
   // todo: 在 initializecomponent 調(diào)用后添加任何構(gòu)造函數(shù)代碼
   //
  }
  /// <summary>
  /// 清理所有正在使用的資源。
  /// </summary>
  protected override void dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null) 
    {
     components.dispose();
    }
   }
   base.dispose( disposing );
  }
  #region windows 窗體設(shè)計器生成的代碼
  /// <summary>
  /// 設(shè)計器支持所需的方法 - 不要使用代碼編輯器修改
  /// 此方法的內(nèi)容。
  /// </summary>
  private void initializecomponent()
  {
   // 
   // form1
   // 
   this.autoscalebasesize = new system.drawing.size(6, 14);
   this.clientsize = new system.drawing.size(208, 150);
   this.name = "form1";
   this.text = "form1";
   this.load += new system.eventhandler(this.form1_load);
  }
  #endregion
  /// <summary>
  /// 應(yīng)用程序的主入口點。
  /// </summary>
  [stathread]
  static void main() 
  {
   application.run(new form1());
  }
  private void form1_load(object sender, system.eventargs e)
  {
   tcpchannel chan = new tcpchannel(7777);
   channelservices.registerchannel(chan);
   remotingconfiguration.registerwellknownservicetype(
    type.gettype("dll.api, dll"),
    "test",
    wellknownobjectmode.singlecall);
  }
 }
}
添加c#windows應(yīng)用程序項目client
global.cs代碼:
using system;
using system.runtime.remoting;
using system.runtime.remoting.channels;
using system.runtime.remoting.channels.tcp;
using dll;
namespace client
{
 /// <summary>
 /// global 的摘要說明。
 /// </summary>
 public class global
 {
  public global()
  {
   //
   // todo: 在此處添加構(gòu)造函數(shù)邏輯
   //
  }
  public static dll.api api
  {
   get
   {
    return (dll.api)activator.getobject(typeof(dll.api), "tcp://"+ config.host + ":" + config.port + "/test");
   }
  }
 }
}
主窗體關(guān)鍵代碼:
  [stathread]
  static void main() 
  {
   application.run(new form1());
   channelservices.registerchannel(new tcpchannel());
  }
  private void form1_load(object sender, system.eventargs e)
  {
   string[] files = global.api.getfiles(@"c:/");//取得服務(wù)器c盤文件
  }
參考文獻:http://support.microsoft.com/default.aspx?scid=kb;zh-cn;307546
商業(yè)源碼熱門下載www.html.org.cn
新聞熱點
疑難解答
圖片精選