简体   繁体   English

如何正确设置 localappdata 路径,不显示完整路径(Path.Combine & Environment.GetFolderPath)

[英]How to set localappdata path properly, not showing the full path(Path.Combine & Environment.GetFolderPath)

I'm trying to Path.Combine, but having highlighted string(appdatapath), helper say's that "a field initializer cannot reference the non-static field, method, or property'MySuperAPP.appdatapath' "我正在尝试 Path.Combine,但突出显示了字符串(appdatapath),助手说“字段初始化程序无法引用非静态字段、方法或属性'MySuperAPP.appdatapath'”

the code is :代码是:

string appdatapath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);

string path = Path.Combine(appdatapath, "second/part/of/folderpath");

what i want is: string path = "C:/Users/USER/AppData/Local/Some/Dir/"我想要的是:字符串路径=“C:/Users/USER/AppData/Local/Some/Dir/”

what i tried :我尝试了什么:

string static appdatapath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);

string static path = Path.Combine(appdatapath,"second/part/of/folderpath").ToString;

and

public static string GetMyLocalAppDir()
        {
            return Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData).ToString();
        }
string path = Path.Combine(GetMyLocalAppDir(),"second/part/of/folderpath").ToString;

i think the variants that i'm tried may be wrong..) need your advice) thank's!)我认为我尝试的变体可能是错误的..)需要你的建议)谢谢!)

When you initialize a field (this mean: when you provide a dynamic field with a value at runtime ) it must be a static value.当您initialize一个字段时(这意味着:当您在runtime为一个动态字段提供一个值时)它必须是一个static值。 Therefore you must declare "appdatapath" as static .因此,您必须将“appdatapath”声明为static

public partial class MainWindow : Window
{
    private static string appdatapath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);

    private (static) string path = System.IO.Path.Combine(appdatapath, "second/part/of/folderpath"); //make this static if you want that this field can't be changed.
    public MainWindow()
    {
        InitializeComponent();
    }
}

Also make sure your declaration is in the right order:还要确保您的声明顺序正确:

public or private static or not type eg string name of variable public or private static or not类型,例如name of variable type eg string名称

Finaly: if you have more directory's to combine, put eacht part separately:最后:如果您有更多目录要组合,请将每个部分分开放置:

Path.Combine(appdatapath, "second", "part", "of", "folderpath")
    public static void Read_bootup3_file()
    {
        qq = 0;
        string downloadz2;

        string fileNameSDcard = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "download.txt");
        string CurrentContents;
        CurrentContents = fileNameSDcard;
//CurrentContents = File.ReadAllText(fileNameSDcard);
        File.WriteAllText(fileNameSDcard, CurrentContents);

        using (StreamReader sr = new StreamReader(fileNameSDcard))
        {
            downloadz2 = sr.ReadToEnd();
        }
//downloadz = downloadz2.ToCharArray();

        qq = 0;
        for (leep = 0; leep <= lotsize; leep++)
        {
            for (lep = 0; lep <= 20; lep++)
            {
                Tester.garage_array_database[lep, leep] = downloadz[qq].ToString();
                qq++;
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM