winforms - C# DirectoryNotFoundException was unhandled in Windows 10 machine -
c# application.exe not working in win10,but worked on win7. tried debug in win10 shows me error correct in win7.
using system; using system.windows.forms; using system.io; namespace windowsformsapplication2 { public partial class form1 : form { public form1() { initializecomponent(); string fullcomputername = environment.machinename; //create folder path string createfolderpath = @"c:\\users\\" +fullcomputername+"\\documents\\cheques"; //create file inside of folder string createtxtfile= createfolderpath + "\\chequefordeposit.txt"; try { if(!directory.exists(createfolderpath)) { return; } directory.createdirectory(createfolderpath); } catch { } { } if(!file.exists(createtxtfile)) { file.create(createtxtfile); }//the error here } } }
when check in win7 pc, creates folder , text file. not in win10. strange.
your try/catch not ensure directory folder exist (may generate exception when try create folder). before creating file, check folder exists. condition incorrect. if folder not exists should return, otherwise create.
try { if(directory.exists(createfolderpath) && !file.exists(createtxtfile)) { file.create(createtxtfile); } }
also check permission issue. e.g. check permissions desktop folder. in windows explorer right click on desktop folder, select properties , there go security tab. should have write permission folder.
Comments
Post a Comment