Skip to content

Instantly share code, notes, and snippets.

@NaserKhoshfetrat
Created August 22, 2021 12:02
Show Gist options
  • Save NaserKhoshfetrat/dd7dfe34db0eecc086f81b5cea74dfd1 to your computer and use it in GitHub Desktop.
Save NaserKhoshfetrat/dd7dfe34db0eecc086f81b5cea74dfd1 to your computer and use it in GitHub Desktop.
c# file and folder method
public class DirectoryAndFileUtils
{
public byte[] ReadFile(string path)
{
try
{
byte[] readBuffer = File.ReadAllBytes(path);
return readBuffer;
}
catch (IOException e)
{
throw e;
}
}
public void CopyFile(string sourceFileToCopyPath, string destinationPath)
{
if (IsFileExist(sourceFileToCopyPath) == true)
{
File.Copy(sourceFileToCopyPath, destinationPath);
}
}
public void SaveFile(string path, byte[] file)
{
using (FileStream fs = File.Create(path))
{
for (byte i = 0; i < file.Length; i++)
{
fs.WriteByte(file[i]);
}
}
}
public string GetFilfeName(string path)
{
return Path.GetRandomFileName();
}
public bool IsFileExist(string path)
{
return File.Exists(path);
}
public DirectoryInfo CreateFolder(string path)
{
return Directory.CreateDirectory(path);
}
public void SaveTextFile(string folderPath, string fileName, string text)
{
if (!IsFolderExist(folderPath))
{
CreateFolder(folderPath);
}
var filePath = Path.Combine(folderPath, fileName);
FileStream fParameter = new FileStream(filePath, FileMode.Create, FileAccess.Write);
StreamWriter m_WriterParameter = new StreamWriter(fParameter);
m_WriterParameter.BaseStream.Seek(0, SeekOrigin.End);
m_WriterParameter.Write(text);
m_WriterParameter.Flush();
m_WriterParameter.Close();
}
public void DeleteFolder(string path)
{
if (IsFolderExist(path))
{
Directory.Delete(path);
}
}
public bool IsFolderExist(string path)
{
return Directory.Exists(path);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment