示例#1
0
        public void Close()
        {
            if (tr != null)
            {
                tr.Close();
            }

            if (tw != null)
            {
                tw.Close();
            }

            tr = null;
            tw = null;
        }
 /// <summary>
 /// 通过密钥对文本进行加密
 /// </summary>
 /// <param name="str"></param>
 /// <param name="SecretKey">密钥</param>
 /// <param name="SingleLine">True:单行;False:所有</param>
 /// <param name="Standard">是否使用标准方式</param>
 /// <returns>加密后的文本</returns>
 public static string Encrypt(this string str, string SecretKey, bool SingleLine = true, bool Standard = false)
 {
     DESCryptoServiceProvider des = new DESCryptoServiceProvider();
     if (Standard)
     {
         des.Mode = CipherMode.ECB;
         des.Padding = PaddingMode.Zeros;
     }
     des.Key = SecretKey.ToMD5().Substring(0, 0x08).ToBytes();
     des.IV = SecretKey.ToMD5().Substring(0, 0x08).ToBytes();
     global::System.IO.MemoryStream ms = new global::System.IO.MemoryStream();
     CryptoStream encStream = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
     global::System.IO.StreamWriter sw = new global::System.IO.StreamWriter(encStream);
     if (SingleLine)
         sw.WriteLine(str);
     else
         sw.Write(str);
     sw.Close();
     encStream.Close();
     byte[] buffer = ms.ToArray();
     ms.Close();
     StringBuilder hash = new StringBuilder();
     foreach (byte b in buffer.ToArray())
     {
         hash.AppendFormat("{0:X2}", b);
     }
     return hash.ToString();
 }
示例#3
0
        public bool Replace(string line, string to)
        {
            if (tw == null)
            {
                return(false);
            }

            try
            {
                FileInfo fi = new FileInfo(_path);
                fi.CopyTo(_path + ".tmp");

                tw.Close();
                tw = new StreamWriter(_path, false);

                StreamReader sr = new StreamReader(_path + ".tmp");
                string       l, id = "";
                while ((l = sr.ReadLine()) != null)
                {
                    int pos = l.IndexOf(":");
                    if (pos != -1)
                    {
                        id = l.Substring(0, pos);
                        l  = l.Substring(pos + 1, l.Length - pos - 1);
                    }
                    if (l == line)
                    {
                        tw.WriteLine(id + ":" + to);
                    }
                    else
                    {
                        tw.WriteLine(id + ":" + l);
                    }
                }
                sr.Close();
                tw.Close();
                fi = new FileInfo(_path + ".tmp");
                fi.Delete();

                tw = new StreamWriter(_path, true);
                return(true);
            }
            catch
            {
                return(false);
            }
        }
示例#4
0
        public bool ReplaceHoleLine(string line, string to)
        {
            if (tw == null)
            {
                return(false);
            }

            try
            {
                bool     ret = false;
                FileInfo fi  = new FileInfo(_path);
                fi.CopyTo(_path + ".tmp");

                tw.Close();
                tw = new StreamWriter(_path, false);

                StreamReader sr = new StreamReader(_path + ".tmp");
                string       l;
                while ((l = sr.ReadLine()) != null)
                {
                    if (l == line)
                    {
                        tw.WriteLine(to);
                        ret = true;
                    }
                    else
                    {
                        tw.WriteLine(l);
                    }
                }
                sr.Close();
                tw.Close();
                fi = new FileInfo(_path + ".tmp");
                fi.Delete();

                tw = new StreamWriter(_path, true);
                return(ret);
            }
            catch
            {
                return(false);
            }
        }
示例#5
0
        private static void menuCommonLVExportToTextFile_Click(
            object sender,
            EventArgs e)
        {
            var lvCtx = GetCommonLVContext(sender);
            if (lvCtx == null)
                return;

            var lv = lvCtx.lv;

            string fn = Dialogs.FileSysDialogs.BrowseFile(
                lvCtx.options.ExportDefaultDirectory?? Path.GetDirectoryName((new FileInfo(global::System.Reflection.Assembly.GetExecutingAssembly().Location)).FullName),
                "",
                lvCtx.options.ExportDefaultExtensions,
                lvCtx.options.ExportDefaultFilter,
                true);
            if (string.IsNullOrEmpty(fn))
                return;

            using (TextWriter Out = new global::System.IO.StreamWriter(fn, false))
            {
                // Write column header
                foreach (MSWinForms.ColumnHeader Cur in lv.Columns)
                {
                    Out.Write("\"" + Cur.Text + "\"");
                    Out.Write(lvCtx.options.CopyItemsSeparator);
                }
                Out.WriteLine();

                foreach (MSWinForms.ListViewItem Item in lv.Items)
                    Out.WriteLine(Item.GetItemsString());

                Out.Close();
            }
        }