|
File.WriteAllText(@"C:\Dossier\fichier.txt", "Texte à écrire");
string[] lines = { "First line", "Second line", "Third line" };
File.WriteAllLines(@"C:\Dossier\fichier.txt", lines);
if (File.Exists(@"C:\Dossier\fichier.txt"))
// test si un nom de fichier est valide
if (String.IsNullOrWhiteSpace(fileName) || fileName.IndexOfAny(Path.GetInvalidFileNameChars()) >= 0)
// renommer un fichier. La destination doit être un fichier.
File.Move(@"C:\Dossier\fichier.txt", @"C:\Dossier\fichier2.txt");
File.Copy(@"C:\Dossier\fichier.txt", @"C:\Dossier\fichier2.txt");
File.Delete(@"C:\Dossier\fichier.txt");
|
Directory
|
Directory.CreateDirectory("C:\dossier\sous-dossier");
string[] filePaths = Directory.GetFiles("/chemin/vers/un/dossier", "*.txt", SearchOption.AllDirectories);
string[] folderPaths = Directory.GetDirectories("/chemin/vers/un/dossier", "*", SearchOption.AllDirectories);
string[] folderPaths = new DirectoryInfo("/chemin/vers/un/dossier").GetDirectories()
.Where(di => !di.Attributes.HasFlag(FileAttributes.ReparsePoint)).Select(di => di.Name);
|
Path
|
using System.IO;
var filePath = @"C:\Dossier\fichier.txt"
Path.GetDirectoryName(filePath);
Path.GetFileName(Path.GetDirectoryName(filePath));
Path.GetExtension(filePath);
Path.GetFileName(filePath);
Path.GetFileNameWithoutExtension(filePath);
Path.Combine(@"C:\AutreDossier", "fichier.odt");
var cheminAbsolu = Path.GetFullPath(cheminRelatif)
|
Caractères interdits
|
var replacement = "";
var fileName = Path.GetInvalidFileNameChars().Aggregate(fileName, (current, c) => current.Replace(c.ToString(), replacement));
|
|
public static string GetRelativePath(string fromPath, string toPath)
{
int fromAttr = GetPathAttribute(fromPath);
int toAttr = GetPathAttribute(toPath);
StringBuilder path = new StringBuilder(260);
if(PathRelativePathTo(
path,
fromPath,
fromAttr,
toPath,
toAttr) == 0)
{
throw new ArgumentException("Paths must have a common prefix");
}
return path.ToString();
}
private static int GetPathAttribute(string path)
{
DirectoryInfo di = new DirectoryInfo(path);
if (di.Exists)
{
return FILE_ATTRIBUTE_DIRECTORY;
}
FileInfo fi = new FileInfo(path);
if(fi.Exists)
{
return FILE_ATTRIBUTE_NORMAL;
}
throw new FileNotFoundException();
}
private const int FILE_ATTRIBUTE_DIRECTORY = 0x10;
private const int FILE_ATTRIBUTE_NORMAL = 0x80;
[DllImport("shlwapi.dll", SetLastError = true)]
private static extern int PathRelativePathTo(StringBuilder pszPath,
string pszFrom, int dwAttrFrom, string pszTo, int dwAttrTo);
|
PathRelativePathTo function
- Les chemins avec dossiers + fichier sont d'une longueur maximale de 260
- Les chemins avec dossiers sans fichiers sont d'une longueur maximale de 248 (260-12)
Dossiers spéciaux de Windows
|
string path = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
|
Énumération SpecialFolder
|
Chemin correspondant (7 64bits)
|
Windows
|
C:\Windows
|
System
|
C:\Windows\System32
|
SystemX86
|
C:\Windows\SysWOW64
|
UserProfile
|
C:\Users\<User Name>
|
MyDocuments
|
C:\Users\<User Name>\Documents
|
ApplicationData
|
C:\Users\<User Name>\AppData\Roaming
|
LocalApplicationData
|
C:\Users\<User Name>\AppData\Local
|
InternetCache
|
C:\Users\<User Name>\AppData\Local\Microsoft\Windows\Temporary Internet Files
|
ProgramFiles
|
C:\Program Files (x86)
|
WinForm
Ces propriétés ne sont disponible que pour les application WinForm.
|
string path = Application.UserAppDataPath;
string path = Application.LocalUserAppDataPath;
|
|
private static void DirectoryCopy(string sourcePath, string destinationPath, bool copySubDirectories)
{
var directory = new DirectoryInfo(sourcePath);
var directories = directory.GetDirectories();
if (!directory.Exists)
{
throw new DirectoryNotFoundException("Source directory does not exist or could not be found: " + sourcePath);
}
if (!Directory.Exists(destinationPath))
{
Directory.CreateDirectory(destinationPath);
}
var files = directory.GetFiles();
foreach (FileInfo file in files)
{
string destinationFilePath = Path.Combine(destinationPath, file.Name);
var destinationFile = new FileInfo(destinationFilePath);
if (destinationFile == null)
{
file.CopyTo(destinationFilePath, false);
}
else if (file.LastWriteTime > destinationFile.LastWriteTime)
{
file.CopyTo(destinationFilePath, true);
}
}
if (copySubDirectories)
{
foreach (DirectoryInfo subDirectory in directories)
{
string destinationFolderPath = Path.Combine(destinationPath, subDirectory.Name);
DirectoryCopy(subDirectory.FullName, destinationFolderPath, copySubDirectories);
}
}
}
|
Uri et Webdav
|
string path = @"\\serveur@SSL\Webdav\Fichier.txt";
new Uri(path);
WebBrowser.Source = new Uri(path);
WebBrowser.NavigateToStream((new StreamReader(path)).BaseStream);
XDocument.Load(path);
XDocument.Load(new StreamReader(path));
var xd = new XmlDocument();
xd.Load(path);
xd.Load(new StreamReader(path));
|