C# - Ways to rename a directory -
this question has answer here:
- can't rename directory in c# manually 2 answers
i'm using directory.move(olddir, newdir)
rename directory. every , ioexception
saying "access path 'olddir' denied". if right click directory in explorer can rename without issues.
so question is: there other ways rename directory without using directory.move
?
i thought of using command shell (process.start()
) should last way of doing it.
further details
the program still running, exception , can rename manually in windows explorer (right click -> rename) while cursor paused on breakpoint in catch block. tried setting breakpoint @ directory.move
, renamed directory in explorer (and again), stepped on directory.move
, ended in catch (ioexception)
again.
here code
public bool copy() { string destpathrelease = thisuser.destpath + "\\release"; if (directory.exists(destpathrelease)) { try { string newpath = thisuser.destpath + '\\' + (string.isnullorempty(currbuildlabel) ? ("release" + '_' + datetime.now.tostring("yyyymmdd_hhmmss")) : currbranchname) + '.' + currbuildlabel; directory.move(destpathrelease, newpath); } catch (ioexception) { // breakpoint } } }
as can see entered method. never touched directory in program before.
since way not working me need find way so.
solution
public bool copy() { string destpathrelease = thisuser.destpath + "\\release"; shfileopstruct struc = new shfileopstruct(); struc.hnamemappings = intptr.zero; struc.hwnd = intptr.zero; struc.lpszprogresstitle = "rename release directory"; struc.pfrom = destpathrelease + '\0'; struc.pto = thisuser.destpath + '\\' + (string.isnullorempty(this.currbuildlabel) ? ("release" + '_' + datetime.now.tostring("yyyymmdd_hhmmss")) : this.currbranchname) + '.' + this.currbuildlabel + '\0'; struc.wfunc = filefuncflags.fo_rename; int ret = shfileoperation(ref struc); }
please note it's important use pto
, pfrom
0 delimited double 0 terminated strings.
you try using p/invoke call shfileoperation
api (or ifileoperation
interface). explorer uses in theory, should replicate functionality more directly.
Comments
Post a Comment