小编use*_*526的帖子

运行控制台应用程序作为服务

我开发了c#application,其中apllication输出类型是Console Applicatiuon.我想将此应用程序作为服务运行.当我从visual studio运行它或只需双击.exe时,Environment.UserInteractive始终为true.

以下是我的代码

 static void Main(string[] args)
        {
            // Get the version of the current application.
            Assembly assem = Assembly.GetExecutingAssembly();
            AssemblyName assemName = assem.GetName();
            Version ver = assemName.Version;
            // Console.WriteLine("{0}, Version {1}", assemName.Name, ver.ToString());

            Console.WriteLine("{0} version {1}", assemName.Name, ver.ToString());

            TouchService touchService = new TouchService();


            if (Environment.UserInteractive)
            {
                bool show_help = false;
                bool install_service = false;
                bool uninstall_service = false;
                string servicename = "";

                OptionSet p = new OptionSet()                  
                  .Add("h|?|help", delegate(string v) { show_help = v != null; })
                  .Add("s|servicename=", …
Run Code Online (Sandbox Code Playgroud)

c# console

14
推荐指数
4
解决办法
4万
查看次数

如何修复加载配置文件时"配置系统初始化失败/ Root元素丢失"错误?

我在c#windows应用程序中遇到此错误:"配置系统无法初始化".

它工作正常.突然间我得到了这个例外.它显示内部异常细节为"缺少根元素".(C:\ Users\company\AppData\Local\Clickbase_Corp_Sverige_AB\TouchStation.vshost.exe_Url_no1nets4fg3oy2p2q2pnwgulbvczlv33\1.1.0.12\user.config)"}.当我尝试从Settings.cs类中获取值时会发生这种情况.

在program.cs文件中,编写了以下代码

if (Properties.Settings.Default.CallUpgrade)
            {
                Properties.Settings.Default.Upgrade();
                Properties.Settings.Default.CallUpgrade = false;
                Properties.Settings.Default.Save();                
            }
Run Code Online (Sandbox Code Playgroud)

并调用settings.cs类,其中下面的代码抛出异常

    [global::System.Configuration.UserScopedSettingAttribute()]
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
    [global::System.Configuration.DefaultSettingValueAttribute("True")]

    public bool CallUpgrade {
        get {
            return ((bool)(this["CallUpgrade"]));
        }
        set {
            this["CallUpgrade"] = value;
        }
    }
Run Code Online (Sandbox Code Playgroud)

以下是我的整个app.config

<configuration>
  <configSections>
    <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <section name="TouchStation.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
      <section name="TouchStation.TouchStation" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
    </sectionGroup>
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <section name="TouchStation.TouchStation" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> …
Run Code Online (Sandbox Code Playgroud)

c# config

10
推荐指数
3
解决办法
5万
查看次数

删除sqlite db文件

我正在开发一个 C# 应用程序,其后端为 sqlite。在我的应用程序中,我有一个用于清理数据库的按钮(删除 .db 文件并使用一些初始数据再次创建它)。有时,当我尝试删除数据库时,它说无法删除它,因为它正在被另一个进程使用。在删除它之前,我使用了关闭连接、处理和清除池功能。即使那样,它也会抛出相同的异常。这是我的代码:

string targetDataBaseFilePath = Path.Combine(dataDirectoryPath, "local.db");               
ThingzDatabase.Create(targetDataBaseFilePath, "touchdb");
Run Code Online (Sandbox Code Playgroud)

在创建函数中,我定义了以下代码,从那里我得到错误:

if (File.Exists(DatabaseFileName))
{
    try
    {
        ThingzDatabase.localdb.conn.Close();
        ThingzDatabase.localdb.conn.Dispose();
        SQLiteConnection.ClearAllPools();
    }
    catch (Exception)
    {
    }
    File.Delete(DatabaseFileName); //error throws from here.                       
}
Run Code Online (Sandbox Code Playgroud)

如何防止错误被抛出?

sqlite sqlexception drop-database c#-4.0

6
推荐指数
2
解决办法
1万
查看次数

使用 C# 获取当前国家和位置详细信息

我想知道我的应用程序在哪里使用。

这是获取国家/地区名称和时区的代码:

TimeZone localZone = TimeZone.CurrentTimeZone;
var result = localZone.StandardName;
var s = result.Split(' ');
Console.WriteLine(s[0]);
Console.WriteLine(RegionInfo.CurrentRegion.DisplayName);
Run Code Online (Sandbox Code Playgroud)

但我的问题是,任何人都可以更改时区。根据时区,我可能会得到错误的名字。并且区域设置为美国,无法更改。因为所有用户都有相同的设置,并且我的应用程序有数十万用户。

有什么方法可以读取任何操作系统设置/系统设置并获取使用我的应用程序的当前国家/地区?

c# regioninfo cultureinfo

2
推荐指数
1
解决办法
9320
查看次数