These days I am working with a webservice which has to work creating, deleting file and folders. In this post i will try to explain how I worked with the elements about File and folders which are in the .net framework.
About Files:
You can work with File or FileInfo both in System.IO . Since my point of view depending what do you need to do is better use one or another. File is for work with files in a quick way, just make an action and forget it, the most of the methods needs the url file and they return void, a quick action like :
File.AppendAllText("c:/error.txt",
"Text to add in the file error.txt");
This line create the file error.txt if it doesn't exist and append the text. there are more methods you can just look it in the VStudio's intellisense. And one advantage is that you don't need to create a File object to work with it, because is a static class.
And the second way FileInfo is maybe a better way if you will work more than once with the same file, if you need give it permissions, or do more than one action, because first you have to create the object:
FileInfo file = new FileInfo("UrlFILE");
and then you can make till I saw more or less the same actions than with File but without put the url each time.
With Directories is more or less the same
, the .net framework has Directory y DirectoryInfo
they have the same use than File and FileInfo, but with the differents with Files.
For example Directory.Copy doesn't Exist, so We had to make our own directory library, for example the Copy method is :
public void CopyDirectory(string url_source,string
url_destine,bool overwrite)
{
if(overwrite)
Directory.Delete(url_destine,true);
if(!Directory.Exists(url_destine))
Directory.CreateDirectory(url_destine);
string path_file_destine = string.Empty;
foreach (string file in Directory.GetFiles(url_source))
{
path_file_destine= url_destine+Path.GetFileName(file);
File.Copy(file, path_file_destine);
}
foreach (string
sub_dir in Directory.GetDirectories(url_source))
CopyDirectory(sub_dir+"/",
dir_Destino+Path.GetFileName(sub_dir)+"/",overwrite);
}
When you can't delete a Directory sometimes is because you are working with it, or another program is working with it or because some file or directory which is in the Directory has ReadOnly permissions, to change a file permissions:
File.SetAttributes("path_file", FileAttributes.Normal);
and for change the Attributes to a directory you need to use DirectoryInfo, it isn't in Directory
Sorry for my English
Luego cuando tenga un rato, lo pongo también en español.