3-4 文件读写例子(4)

2023-01-31 01:01:08
       /// <summary>

       

        public void ReadTextFile(string filepath, out Int16 i, out string filecontent)

        {

            if (File.Exists(filepath))

            {

                try

                {

              StreamReader textreader = new StreamReader(filepath, System.Text.Encoding.Default);

                    filecontent = textreader.ReadToEnd();

                    textreader.Close();

                    i = 1;

                }

                catch

                {

                    i = 2;

                    filecontent = "文件读取错误!";

                }

            }

            else

            {

                i = 0;

                filecontent = "文件或路径无效!";

            }

        }

        /// <summary>

       

        /// <summary>

        public bool DirectoryOption(string Directorypath, string TargetDirectorypath, Int16 OptionMethord, out string[] filesname)

        {

            bool k = true;

            filesname = null;

            if (Directory.Exists(Directorypath))

            {

                try

                {

                    if (OptionMethord == 0)

                    {

                        //创建文件夹

                        Directory.CreateDirectory(Directorypath);

                    }

                    else if (OptionMethord == 1)

                    {

                        //文件夹删除

                        Directory.Delete(Directorypath, true);

                    }

                    else if (OptionMethord == 2)

                    {

                        //文件夹移动

                        Directory.Move(Directorypath, TargetDirectorypath);

                    }

                    else if (OptionMethord == 3)

                    {

                        //获取文件夹下面所有的子文件信息

                        filesname = Directory.GetFiles(Directorypath);

                    }

                }

                catch

                {

                    k = false;

                }

            }

            else

            {

                Directory.CreateDirectory(Directorypath);

                k = true;

            }

            return k;

        }

    }

}

相关文章