如何在所有申请中公开申报? [英] How to declare in public in all the application?

查看:69
本文介绍了如何在所有申请中公开申报?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,
我不知道如何声明一个公共变量,例如我想声明搜索文件并为我的所有应用程序保留相同的值。
下面是我的代码,如何始终拥有名称(DBpath)。
提前谢谢。





我的尝试:



< pre>  //  如果文件夹存在,则搜索磁盘 
string [] drives = Directory.GetLogicalDrives()。其中​​(d = > Directory.Exists(Path.Combine(d, Dossier1)))。ToArray();
string Résult= string .Join( ,drives);


if (Résult!=
{

if (Directory.Exists(Résult+ @ Dossier1 \))
{

if (Directory.Exists(Résult+ @ Dossier1 \Dossier2))
{

if (File.Exists(Résult+ @ Dossier1 \ Dossier2 \ BDD_SQLite.db3))
{

string DBpath =Résult+ @ Dossier1 \Dossier2 \ BDD_SQLite.db3;


}
}
}
}

解决方案

< blockquote>使用公共静态字符串或返回常量字符串的公共静态方法。最好还是使用应用程序设置条目和获取设置值的静态方法。请参阅如何:使用C#|在运行时读取设置Microsoft Docs [ ^ ]


C#不直接支持全局变量,但您可以使用包含全局变量。

参见此处示例: C#如何使用全局变量,字段和函数 [ ^ ]


Hello,
I do not know how to declare a public variable, for example I would like to declare the search for a file and keep for all my application the same value.
Here is my code below, how to always have the name (DBpath).
Thank you in advance.



What I have tried:

<pre>          // search the disks if the folder exists
                        string[] drives = Directory.GetLogicalDrives().Where(d => Directory.Exists(Path.Combine(d, "Dossier1"))).ToArray();
                        string Résult = string.Join(", ", drives);


                        if (Résult != "")
                        {
    
                            if (Directory.Exists(Résult + @"Dossier1\"))
                            {

                                if (Directory.Exists(Résult + @"Dossier1\Dossier2"))
                                {

                                    if (File.Exists(Résult + @"Dossier1\Dossier2\BDD_SQLite.db3"))
                                    {
  
                                        string DBpath = Résult + @"Dossier1\Dossier2\BDD_SQLite.db3";


                                    }
                                }
                            }
                        }

解决方案

Use a public static string, or a public static method that returns a constant string. Better still use an application settings entry and a static method that obtains the settings value. See How To: Read Settings at Run Time With C# | Microsoft Docs[^]


C# does not support global variables directly, but you can use a static class that contains the "global" variables.
See example here: C# How to use Global Variables, Fields and Functions[^]


这篇关于如何在所有申请中公开申报?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆