コード例 #1
0
        public static string ReadFile(string filePath, Encoding encoding)
        {
            if (IsExistFile(filePath))
            {
                try
                {
                    StringBuilder sb = new StringBuilder();
                    using (StreamReader sr = new StreamReader(filePath, encoding))
                    {
                        string temp = string.Empty;
                        while ((temp = sr.ReadLine()) != null)
                        {
                            sb.AppendLine(temp);
                        }
                    }

                    return(sb.ToString());
                }
                catch (Exception ex)
                {
                    CooperationWrapper.WriteLog(ex);
                }
            }
            throw new FileNotFoundException("file not found");
        }
コード例 #2
0
 public static void WriteFileWithEncoding(string filePath, string content, FileMode fileModel, Encoding encoding)
 {
     try
     {
         if (IsExistFile(filePath))
         {
             using (StreamWriter sw = System.IO.File.AppendText(filePath))
             {
                 TextWriter tw = TextWriter.Synchronized(sw);
                 tw.Write(content);
                 tw.Close();
             }
         }
         else
         {
             DirectoryHelper.CreateDir(filePath.Substring(0, filePath.LastIndexOf(@"\")));
             FileStream   fs = System.IO.File.Open(filePath, fileModel, FileAccess.Write);
             StreamWriter sw = new StreamWriter(fs, encoding);
             sw.Flush();
             sw.Write(content);
             sw.Flush();
             sw.Close();
         }
     }
     catch (Exception ex)
     {
         CooperationWrapper.WriteLog(ex);
     }
 }
コード例 #3
0
 public static void Copy(string sourceFileName, string destFileName, bool overwrite)
 {
     try
     {
         System.IO.File.Copy(sourceFileName, destFileName, overwrite);
     }
     catch (Exception ex)
     {
         CooperationWrapper.WriteLog(ex);
     }
 }
コード例 #4
0
 public static bool DeleteFile(string filePath)
 {
     if (IsExistFile(filePath))
     {
         try
         {
             File.Delete(filePath);
             return(true);
         }
         catch (Exception ex)
         {
             CooperationWrapper.WriteLog(ex);
         }
     }
     return(false);
 }
コード例 #5
0
ファイル: DirectoryHelper.cs プロジェクト: orf53975/Common
 public static string[] GetDirectories(string directoryPath, string searchPattern, SearchOption searchOption)
 {
     if (!IsExistDirectory(directoryPath))
     {
         return(new string[] {});
     }
     try
     {
         return(Directory.GetDirectories(directoryPath, searchPattern, searchOption));
     }
     catch (Exception ex)
     {
         CooperationWrapper.WriteLog(ex);
     }
     return(new string[] { });
 }
コード例 #6
0
        public static void AsyncWriteFileWithEncoding(string filePath, string content, FileMode fileModel, Encoding encoding)
        {
            try
            {
                FileStream fs         = null;
                int        bufferSize = 1024;
                //byte[] data = new byte[bufferSize];
                byte[] data = encoding.GetBytes(content);//Encoding.ASCII.GetBytes(content);

                IAsyncResult  result   = null;
                AsyncCallback callback = null;
                callback = asyncresult => {
                    int datalength = (int)asyncresult.AsyncState;
                    fs.EndWrite(asyncresult);
                    datalength -= bufferSize;
                    if (datalength > 0)
                    {
                        fs.BeginWrite(data, data.Length - datalength, datalength < bufferSize ? datalength : bufferSize, callback, datalength);
                    }
                    else
                    {
                        fs.Close();
                    }
                };
                if (IsExistFile(filePath))
                {
                    fs     = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite, FileShare.None, 20480, true);
                    result = fs.BeginWrite(data, 0, bufferSize, callback, data.Length);
                }
                else
                {
                    DirectoryHelper.CreateDir(filePath.Substring(0, filePath.LastIndexOf(@"\")));
                    fs     = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite, FileShare.None, 20480, true);
                    result = fs.BeginWrite(data, 0, bufferSize, callback, data.Length);
                }
            }
            catch (Exception ex)
            {
                CooperationWrapper.WriteLog(ex);
            }
        }
コード例 #7
0
ファイル: DirectoryHelper.cs プロジェクト: orf53975/Common
        public static bool IsEmptyDirectory(string directoryPath)
        {
            try
            {
                if (GetFileNames(directoryPath).Length > 0)
                {
                    return(false);
                }

                if (GetDirectories(directoryPath).Length > 0)
                {
                    return(false);
                }
                return(true);
            }
            catch (IOException ex)
            {
                CooperationWrapper.WriteLog(ex);
            }
            return(true);
        }
コード例 #8
0
ファイル: DirectoryHelper.cs プロジェクト: orf53975/Common
 public static bool CreateDir(string directoryPath)
 {
     CooperationWrapper.IsNullOrEmpty(directoryPath);
     if (Directory.Exists(directoryPath))
     {
         CooperationWrapper.WriteLog(new IOException(string.Format("{0} directory exist", directoryPath)));
         return(true);
     }
     else
     {
         DirectoryInfo info = null;
         try
         {
             info = Directory.CreateDirectory(directoryPath);
         }
         catch (IOException ex)
         {
             CooperationWrapper.WriteLog(ex);
         }
         return(info == null?false:true);
     }
 }
コード例 #9
0
ファイル: DirectoryHelper.cs プロジェクト: orf53975/Common
        public static bool CopyDirectory(string sourcePath, string destinationPath, bool overwriteexisting)
        {
            bool ret = false;

            try
            {
                sourcePath      = sourcePath.EndsWith(@"\") ? sourcePath : sourcePath + @"\";
                destinationPath = destinationPath.EndsWith(@"\") ? destinationPath : destinationPath + @"\";

                if (IsExistDirectory(sourcePath))
                {
                    if (IsExistDirectory(destinationPath) == false)
                    {
                        Directory.CreateDirectory(destinationPath);
                    }

                    foreach (string fls in Directory.GetFiles(sourcePath))
                    {
                        FileInfo flinfo = new FileInfo(fls);
                        flinfo.CopyTo(destinationPath + flinfo.Name, overwriteexisting);
                    }
                    foreach (string drs in Directory.GetDirectories(sourcePath))
                    {
                        DirectoryInfo drinfo = new DirectoryInfo(drs);
                        if (CopyDirectory(drs, destinationPath + drinfo.Name, overwriteexisting) == false)
                        {
                            ret = false;
                        }
                    }
                }
                ret = true;
            }
            catch (Exception ex)
            {
                CooperationWrapper.WriteLog(ex);
                ret = false;
            }
            return(ret);
        }
コード例 #10
0
ファイル: DirectoryHelper.cs プロジェクト: orf53975/Common
 public static bool DeleteDir(string directoryPath)
 {
     CooperationWrapper.IsNullOrEmpty(directoryPath);
     if (!Directory.Exists(directoryPath))
     {
         CooperationWrapper.WriteLog(new IOException(string.Format("{0} directory isn't exist", directoryPath)));
         return(true);
     }
     else
     {
         try
         {
             Directory.Delete(directoryPath, true);
         }
         catch (IOException ex)
         {
             CooperationWrapper.WriteLog(ex);
             return(false);
         }
         return(true);
     }
 }
コード例 #11
0
        public static string AsyncReadFile(string filePath, Encoding encoding)
        {
            if (IsExistFile(filePath))
            {
                try
                {
                    StringBuilder sb         = new StringBuilder();
                    int           bufferSize = 1024;
                    byte[]        data       = new byte[bufferSize];
                    FileStream    fs         = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read, 20480, true);//设置异步调用true
                    AsyncCallback callback   = null;
                    callback = asyncResult =>
                    {
                        FileStream read  = (FileStream)asyncResult.AsyncState;
                        int        bytes = read.EndRead(asyncResult);
                        sb.Append(encoding.GetString(data, 0, bytes));//Encoding.ASCII.GetString(data,0,bytes)
                        //System.Threading.Thread.Sleep(2000);
                        if (bytes > 0)
                        {
                            read.BeginRead(data, 0, bufferSize, callback, read);
                        }
                        else
                        {
                            read.Close(); Console.WriteLine(sb.ToString());
                        }
                    };

                    IAsyncResult async = fs.BeginRead(data, 0, bufferSize, callback, fs);
                    return(sb.ToString());
                }
                catch (Exception ex)
                {
                    CooperationWrapper.WriteLog(ex);
                }
            }
            throw new FileNotFoundException("file not found");
        }
コード例 #12
0
 public static bool IsExistFile(string filename)
 {
     CooperationWrapper.IsNullOrEmpty(filename);
     return(File.Exists(filename));
 }
コード例 #13
0
ファイル: DirectoryHelper.cs プロジェクト: orf53975/Common
 public static bool IsExistDirectory(string directoryPath)
 {
     CooperationWrapper.IsNullOrEmpty(directoryPath);
     return(Directory.Exists(directoryPath));
 }