zoukankan      html  css  js  c++  java
  • 获取上次打开目录

    路径简单保存到csv文件的方式,下次直接获取。

    调用方法:

    try
    {
          dialog.SelectedPath = FolderPathHelper.GetLastPath("9009","菜单1");
    }
     catch (Exception ex)
     {
          Log($"获取路径失败:{ex.ToString()}");
    #if DEBUG
          throw ex;
    #endif
    }
    try
    {
         FolderPathHelper.SavePath("9009","菜单1","C:\");
    }
    catch (Exception ex)
    {
          Log($"保存路径失败:{ex.ToString()}");
    #if DEBUG
           throw ex;
    #endif
    }    

    保存与获取的类:

    public class FolderPathHelper
        {
            public static string GetLastPath(string user, string menu)
            {
                string currentPath = Directory.GetCurrentDirectory() + "\UserPath\LastPath.csv";
    
                if (!File.Exists(currentPath))
                {
                    return "";
                }
    
                var table = OpenCSVFile(currentPath);
                var folder = table.AsEnumerable().FirstOrDefault(row => row["user"].ToString() == user && row["menu"].ToString() == menu)?["folder"].ToString() ?? "";
                return folder;
            }
    
            public static bool SavePath(string user, string menu, string folder)
            {
                //判断目录是否存在
                string currentPath = Directory.GetCurrentDirectory() + "\UserPath\LastPath.csv";
                DataTable table;
    
                table = OpenCSVFile(currentPath);
                if (table.Rows.Count > 0)
                {
                    var rows = table.AsEnumerable().Where(row => row["user"].ToString() == user && row["menu"].ToString() == menu).ToList();
                    foreach (var row in rows)
                    {
                        table.Rows.Remove(row);
                    }
                }           
                table.Rows.Add(user, menu, folder, DateTime.Now.ToString("yyyyMMdd hhmmss"));
                SaveCSV(table, currentPath);
                return true;
            }
    
            public static void SaveCSV(DataTable dt, string fullPath)//table数据写入csv
            {
                System.IO.FileInfo fi = new System.IO.FileInfo(fullPath);
                if (!fi.Directory.Exists)
                {
                    fi.Directory.Create();
                }
                using (System.IO.FileStream fs = new System.IO.FileStream(fullPath, System.IO.FileMode.Create,
                    System.IO.FileAccess.Write))
                using(System.IO.StreamWriter sw = new System.IO.StreamWriter(fs, System.Text.Encoding.UTF8))
                {
                    for (int i = 0; i < dt.Rows.Count; i++) //写入各行数据
                    {
                        var data = new List<string>();
                        for (int j = 0; j < dt.Columns.Count; j++)
                        {
                            var str = dt.Rows[i][j].ToString();
                            //替换英文冒号 英文冒号需要换成两个冒号
                            str = str.Replace(""", """");
                            //含逗号 冒号 换行符的需要放到引号中
                            if (str.Contains(',') || str.Contains('"') || str.Contains('
    ') || str.Contains('
    '))                        
                            {
                                str = $""{str}"";
                            }
                            data.Add(str);                       
                        }                    
                        sw.WriteLine(string.Join(",",data));
                    }
                    sw.Close();
                    fs.Close();
                }               
            }
    
            private static DataTable OpenCSVFile(string filepath)
            {
                string strpath = filepath; //csv文件的路径
                var table = new DataTable();
                table.Columns.Add("user");
                table.Columns.Add("menu");
                table.Columns.Add("folder");
                table.Columns.Add("time");
    
                if (!File.Exists(filepath))
                {
                    return table;
                }
    
                string strline;
                string[] aryline;
                using (StreamReader mysr = new StreamReader(strpath, System.Text.Encoding.Default))
                {
                    while ((strline = mysr.ReadLine()) != null)
                    {
                        aryline = strline.Split(new char[] { ',' });
                        var row = table.NewRow();
                        for (int i = 0; i < table.Columns.Count; i++)
                        {
                            row[i] = aryline[i];
                        }
                        table.Rows.Add(row);
                    }
                    return table;
                }
            }
        }
  • 相关阅读:
    hibernate各种状态
    Persistence createEntityManagerFactory方法使用
    JS数组学习笔记
    ES6笔记之参数默认值(译)
    JS是按值传递还是按引用传递?
    linux awk命令详解
    Linux Shell笔记之sed
    类似微信红包随机分配js方法
    ionic tabs隐藏完美解决
    mustache 获取json数据内数组对象指定元素的方法
  • 原文地址:https://www.cnblogs.com/bibi-feiniaoyuan/p/13403593.html
Copyright © 2011-2022 走看看