c# winform用
A:“Application.StartupPath”:获取当前应用程序所在目录的路径,最后不包含“”;
B:“Application.ExecutablePath ”:获取当前应用程序文件的路径,包含文件的名称;
C:“AppDomain.CurrentDomain.BaseDirectory”:获取当前应用程序所在目录的路径,最后包含“”;
D:“System.Threading.Thread.GetDomain().BaseDirectory”:获取当前应用程序所在目录的路径,最后包含“”;
E:“Environment.CurrentDirectory”:获取当前应用程序的路径,最后不包含“”;
F:“System.IO.Directory.GetCurrentDirectory”:获取当前应用程序的路径,最后不包含“”;
//恢复上次位置 void LoadCount() { StreamReader slog = new StreamReader(LogFilePath, Encoding.Default);// string countLog = slog.ReadLine(); if ("" != countLog && null != countLog) count = int.Parse(countLog) - 1;//下标从0开始计算的 slog.Close(); }
//读取最高分数 void LoadScoreMax() { string fname = @"" + AppDomain.CurrentDomain.BaseDirectory + "ScoreFile"; string FilePath = Path.Combine(fname, "score.log"); if (!File.Exists(FilePath)) WriteFile(); StreamReader slog = new StreamReader(FilePath, Encoding.Default);// string scoreLog = slog.ReadLine(); if ("" != scoreLog && null != scoreLog) { ScoreMax = int.Parse(scoreLog); } slog.Close(); }
//保存记录 void WriteFile() { string fname = @"" + AppDomain.CurrentDomain.BaseDirectory + "ScoreFile"; if (!Directory.Exists(fname))//如果路径不存在 { Directory.CreateDirectory(fname);//创建一个路径的文件夹 } StreamWriter writer = new StreamWriter(Path.Combine(fname,"score.log"), false, System.Text.Encoding.Default); writer.WriteLine(ScoreMax.ToString()); writer.Close(); }