/// <summary>
        /// 将文件内容设置到文本框中
        /// </summary>
        /// <param name="textBox">文本框 为null则打开一个新标签</param>
        /// <param name="path">文本框路径</param>
        /// <param name="encoding">编码</param>
        public static void SetTextBoxValByPath(TextBox textBox, string path, Encoding encoding)
        {
            TextBox t = textBox;

            // 传入的文本框为null则新建一个标签
            if (path == null)
            {
                return;
            }
            // 判断是否为二进制文件
            if (!CheckIsTextFile(path))
            {
                DialogResult dr = MessageBox.Show("该文件可能为二进制文件,是否继续读取?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (dr == DialogResult.Cancel)
                {
                    return;
                }
                if (dr == DialogResult.No)
                {
                    return;
                }
            }
            // 将文件内容赋值到文本框中
            t.Text           = FileRead.Read(path, encoding);
            t.SelectionStart = t.TextLength;

            // 将文件路径写入到文本框tag数据中
            TextBoxUtils.TextBoxAddTag(t, TextBoxTagKey.SAVE_FILE_PATH, path);
            // 将文件编码写入到文本框tag数据中
            TextBoxUtils.TextBoxAddTag(t, TextBoxTagKey.TEXTBOX_TAG_KEY_ECODING, encoding);
            // 监听文件变化并弹窗提醒
            monitorFileTextShowMess(path, t);
            t.Focus();
        }
        public void DiffTests() //Should fail, as fileRead should handle invalid file names
        {
            List <List <string[]> > contents        = new List <List <string[]> >();
            List <List <string[]> > invalidContents = new List <List <string[]> >();

            contents        = FileRead.Read("test1.txt", "test2.txt");
            invalidContents = FileRead.Read("notA.txt", "realFile.txt");

            Assert.ThrowsException <System.Exception>(() => Diff.Difference(contents));
            Assert.ThrowsException <System.Exception>(() => Diff.Difference(invalidContents));
        }
Exemple #3
0
    public static void Main(string[] args)
    {
        FileRead fr = new FileRead(args[0]);

        byte[]        buffer = new byte[128];
        ASCIIEncoding e      = new ASCIIEncoding();

        // loop through, read until done
        Console.WriteLine("Contents");
        while (fr.Read(buffer, 128) != 0)
        {
            Console.Write("{0}", e.GetString(buffer));
        }
    }
        public void FileReadTests() //Should fail, as invalid file names should be handled gracefully
        {
            string[] inputOne = new string[2];
            string[] inputTwo = new string[2];
            inputOne[0] = "test1.txt";
            inputOne[1] = "test2.txt";
            inputTwo[0] = "noFile.txt";
            inputTwo[1] = "namedThis.txt";

            for (int i = 0; i < inputOne.Length; i++)
            {
                Assert.ThrowsException <System.Exception>(() => FileRead.Read(inputOne[i], inputTwo[i]));
            }
        }
Exemple #5
0
 public void ReadTest()
 {
     Assert.AreNotEqual(_file.Read("DrinkList"), FileRead.ERROR);
     Assert.AreEqual(_file.Read("DrinkList", "sas"), FileRead.ERROR);
     Assert.AreEqual(_file.Read("DrinkList", "asd", "asd"), FileRead.ERROR);
     Assert.AreEqual(_file.Read("asda"), FileRead.ERROR);
     Assert.AreEqual(_file.Read("asda", ".das"), FileRead.ERROR);
     Assert.AreEqual(_file.Read("asda", ".das", "das"), FileRead.ERROR);
 }
    public static void Mainly()
    {
        FileRead fr = new FileRead(@"..\..\readfileunsafe.cs");

        byte[]        buffer = new byte[128];
        ASCIIEncoding e      = new ASCIIEncoding();

        // loop through, read until done...
        Console.WriteLine("Contents");
        int bytesRead = 0;

        while ((bytesRead = fr.Read(buffer, buffer.Length - 1)) != 0)
        {
            Console.Write("{0}", e.GetString(buffer, 0, bytesRead));
        }
    }
        /// <summary>
        /// 监听文件变化并弹出提示框提示重新加载或另存为
        /// </summary>
        /// <param name="filepath"></param>
        /// <param name="t"></param>
        public static void monitorFileTextShowMess(string filepath, TextBox t)
        {
            Dictionary <Type, object> data = new Dictionary <Type, object>();

            data.Add(typeof(TextBox), t);

            // 监听文件变化
            try {
                FileSystemWatcher wat      = null;
                string[]          pathArr  = GetPathArr(filepath);
                Encoding          encoding = Encoding.Default;
                if (!"txt".Equals(pathArr[2].ToLower()))
                {
                    encoding = GetType(filepath);
                }
                // 判断文本框的Tag中是否纯在一个监听,存在就销毁他
                if (TextBoxUtils.GetTextTagToMap(t).ContainsKey(TextBoxTagKey.TEXTBOX_TAG_KEY_FILEMONITOR))
                {
                    object obj = TextBoxUtils.GetTextTagToMap(t)[TextBoxTagKey.TEXTBOX_TAG_KEY_FILEMONITOR];
                    if (obj.GetType().Equals(typeof(FileSystemWatcher)))
                    {
                        wat = (FileSystemWatcher)TextBoxUtils.GetTextTagToMap(t)[TextBoxTagKey.TEXTBOX_TAG_KEY_FILEMONITOR];
                        wat.EnableRaisingEvents = false;
                        wat.Dispose();
                    }
                }

                // 获取一个新的文件监听
                wat = fileMonitor(pathArr[0], pathArr[1] + "." + pathArr[2],
                                  NotifyFilters.DirectoryName | NotifyFilters.FileName | NotifyFilters.Size
                                  , null, null
                                  , delegate(object sender, FileSystemEventArgs e){
                    FileSystemWatcher watcher = (FileSystemWatcher)sender;
                    // 闪烁窗体
                    Form f = t.FindForm();
                    if (f.InvokeRequired)
                    {
                        f.Invoke(new EventHandler(delegate {
                            System.Media.SystemSounds.Asterisk.Play();
                            WindowsApiUtils.FlashWindow(f.Handle, 400, 3);
                        }));
                    }
                    // 弹出对话框
                    ControlsUtils.ShowAskMessBox("文件内容已经更改,是否要重新加载文件", "提示"
                                                 , delegate {
                        if (t.InvokeRequired)
                        {
                            t.Invoke(new EventHandler(delegate {
                                // 获取内容
                                string text = FileRead.Read(filepath, encoding);
                                t.Text      = text;
                            }));
                        }
                    }, null);
                    watcher.EnableRaisingEvents = false;
                }
                                  , delegate { // 弹出对话框
                    MessageBox.Show("文件在磁盘上已经被删除或重命名!");
                });
                // 加入到文本框的tag数据中
                TextBoxUtils.TextBoxAddTag(t, TextBoxTagKey.TEXTBOX_TAG_KEY_FILEMONITOR, wat);
            } catch {
                MessageBox.Show("监听文件状态时发生异常");
            }
        }