【问题标题】:How to overcome this situation Windows Services App如何克服这种情况 Windows Services App
【发布时间】:2013-07-16 07:13:21
【问题描述】:

我已经在受保护的 OnStart 中按用户名创建了文件夹,但我想用 Onstart 方法创建我的代码正在运行,但我不知道用 OnStart 方法创建。你能帮我吗?谢谢你。

protected override void OnStart(string[] args)
{
    this.myTimer.Enabled = true;
    string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
    Directory.CreateDirectory(@"c:\ST\" + userName);
}

【问题讨论】:

  • out of OnStartMethod 是什么意思?
  • 需要适当的解释..
  • 我的意思是我想通过 OnStart 中的方法或函数调用下面的代码。我应该在任何方法中编写下面的代码,我应该在受保护的 OnStar 方法中调用此方法。字符串用户名 = System.Security.Principal.WindowsIdentity.GetCurrent().Name; Directory.CreateDirectory(@"c:\ST\" + 用户名);
  • 我迟到了,我是新人,点击回答问题显示警告:您有声誉并预测我已激活会员,我寻找其他问题,因为声誉较高。最后我找到添加评论部分。

标签: c# methods windows-services


【解决方案1】:

@Syaman:我刚刚测试了下面的代码,它可以工作。我希望这就是你要找的。如果是,请将此问题作为已回答。

  protected override void OnStart(string[] args)
  {
      CreatFolder();
  }

  public static void CreatFolder()
  {
      //this.myTimer.Enabled = true;
      string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
      Directory.CreateDirectory(@"c:\ST\" + userName);
  }

【讨论】:

  • @Syaman:无论是控制台应用程序还是服务,上述代码都有效。
【解决方案2】:

我很难理解您的问题,但我认为您正在查看以下内容;如果不能,请您扩展您的问题。

 protected override void OnStart(string[] args)
    {

       testc(); 
    }


    public static void testc()
    {

    }

在您的最后评论之后,我添加了以下代码。 OnStart 方法将启动一个线程并启动所需的方法:

protected override void OnStart(string[] args)
{
    m_Run = true;

    Thread LoadNewData = new Thread(ImportNewData);
    LoadNewData.Start();

}

public void ImportNewData()
{
     while(true)
        {
            //do stuff here
        }
}

【讨论】:

  • 是的,我的意思是这样,但我很好奇如何在 OnStart 中调用此方法(即 testc)。
  • @Syaman:我发现我忘了在 OnStart 方法中调用 testc 方法。我已经更改了上面的代码。
  • 它似乎是内置的,但是当我通过 Windows 服务应用程序在我的计算机上插入它时它不起作用。为什么?
  • 您遇到的错误是什么?如果可能,发布您现在拥有的代码,以便您可以尝试帮助
  • 当然它看起来是正确的,但它在这里不起作用:protected override void OnStart(string[] args) { this.myTimer.Enabled = true;测试(); } public static void testc() { string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name; Directory.CreateDirectory(@"c:\ST\" + 用户名); } 受保护的覆盖无效 OnStop() { this.myTimer.Enabled = false; }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-19
  • 1970-01-01
  • 2011-11-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多