简体   繁体   中英

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' "

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/"

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. Therefore you must declare "appdatapath" as 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

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++;
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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