c# - !File.Exists is not working as expected for filenames containing UTF-8 characters -
my console application (c#) working filenames don't contain utf-8 characters when filenames contain utf-8 character, condition if(!file.exists(destfilepath)) not working expected.
i need delete files present in destination not in source. when there special characters in file name, example,
file
c:\a00000001\20162350775-Étienne geoffroy saint-hilaire, 1772-1844 visionary naturalist. hervé le guyader.pdf
destfilepath
d:\a00000001\20162350775-Étienne geoffroy saint-hilaire, 1772-1844 visionary naturalist. hervé le guyader.pdf
the filename in above case should not deleted both source , destination have same filename did. normal filenames, there no issue. code sample below:
public void synchronizesourceanddestination(string dir) { foreach (string file in directory.getfiles(dir)) { string destfilepath = file.replace(backupdirectory, lookupdirectory); if (!file.exists(destfilepath)) { // delete file backup file.delete(file); } } foreach (string directory in directory.getdirectories(dir)) { string destinationdirectory = directory.replace(backupdirectory, lookupdirectory); if (!directory.exists(destinationdirectory)) { directory.delete(directory, true); continue; } synchronizesourceanddestination(directory); } } note: asp.net web application has setting globalization culture="en-us" uiculture="en-us" requestencoding="utf-8" responseencoding="utf-8" fileencoding="utf-8" in web.config file. above code c# console application process files saved web application. there no issue filenames in local machine when code in server, not working.
it has length of filepath (>260 characters) file.exists work utf-8 characters.
i've tested couple of minutes ago csi.exe, output:
c:\temp>csi microsoft (r) visual c# interactive compiler version 2.2.0.61624 copyright (c) microsoft corporation. rights reserved. type "#help" more information. > system.io.file.exists("c:\\a00000001\\20162350775-Étienne geoffroy saint-hilai re, 1772-1844 visionary naturalist. hervé le guyader.pdf") true > as can see, result true. i've tested on windows 10 machine, dutch language , have vs2017.2 installed.
--edit-- complete comment below, i've created console app test.
using system.io; namespace consoleapp1 { class program { private const string backupdirectory = "c:\\a00000001\\"; private const string lookupdirectory = "c:\\a00000002\\"; static void main(string[] args) { synchronizesourceanddestination("c:\\a00000001\\"); } public static void synchronizesourceanddestination(string dir) { foreach (string file in directory.getfiles(dir)) { string destfilepath = file.replace(backupdirectory, lookupdirectory); if (!file.exists(destfilepath)) { // delete file backup file.delete(file); } } foreach (string directory in directory.getdirectories(dir)) { string destinationdirectory = directory.replace(backupdirectory, lookupdirectory); if (!directory.exists(destinationdirectory)) { directory.delete(directory, true); continue; } synchronizesourceanddestination(directory); } } } } make sure folders a00000001 , a00000002 present on system , place file inside both of them same name , utf-8 characters (20162350775-Étienne geoffroy saint-hilaire, 1772-1844 visionary naturalist. hervé le guyader.pdf).
in case, no file got deleted because of file.exists check.
Comments
Post a Comment