windows - Kernel32 CopyFile does not find a file which exists c# -
i invoked kernel32's copy file method that:
[dllimport("kernel32.dll", charset = charset.unicode, callingconvention = callingconvention.stdcall, setlasterror = true)] [return: marshalas(unmanagedtype.bool)] static extern bool copyfile( [marshalas(unmanagedtype.lpstr)] string lpexistingfilename, [marshalas(unmanagedtype.lpstr)] string lpnewfilename, [marshalas(unmanagedtype.bool)] bool bfailifexists); [dllimport("kernel32.dll")] public static extern uint getlasterror();
however when call it return 2 getlasterror() means file not found. path exists.
string newfile = environment.currentdirectory + "\\temp" + path.getextension(file); uint i; if (!copyfile(file, newfile, true)) = getlasterror();
i'm trying bypass longpath exception solution. doesn't seem work normal files. appreciated.
here's complete code of form1:
using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.windows.forms; using system.io; using shell32; using system.xml; using system.diagnostics; using word = microsoft.office.interop.word; using system.runtime.interopservices; namespace documentcrawler { public partial class form1 : form { [dllimport("kernel32.dll", charset = charset.unicode, callingconvention = callingconvention.stdcall, setlasterror = true)] [return: marshalas(unmanagedtype.bool)] static extern bool copyfile( [marshalas(unmanagedtype.lpstr)] string lpexistingfilename, [marshalas(unmanagedtype.lpstr)] string lpnewfilename, [marshalas(unmanagedtype.bool)] bool bfailifexists); [dllimport("kernel32.dll")] public static extern uint getlasterror(); public form1() { initializecomponent(); } private void btnsearch_click(object sender, eventargs e) { progressbar.style = progressbarstyle.marquee; lbprogress.text = "finding word documents"; btnsearch.enabled = false; lvresults.clear(); searchdirectory(tbdirectory.text, tbfield.text); btnsearch.enabled = true; } void searchdirectory(string path, string searchpattern) { list<string> docs = new list<string>(); foreach (string d in directory.getdirectories(path)) { searchdirectory(path + "\\" + d.remove(0, d.lastindexof('\\') + 1), searchpattern); } foreach (string f in directory.getfiles(path)) { if (path.getextension(f) == ".docx" || path.getextension(f) == ".doc") { docs.add(f); } } progressbar.value = 0; lbprogress.text = "processing word documents 0%"; progressbar.maximum = docs.count; progressbar.style = progressbarstyle.blocks; foreach (string f in docs) { string txt = textfromdocument(f); if (txt.contains(searchpattern)) { lvresults.items.add(f); } progressbar.value++; lbprogress.text = "processing word documents " + ((int)((float)progressbar.value / (float)progressbar.maximum * 100)) + "%"; } } string textfromdocument(string file) { string newfile = environment.currentdirectory + "\\temp" + path.getextension(file); uint i; if (!copyfile(file, newfile, true)) = getlasterror(); object nullobj = system.reflection.missing.value; word.application wordapp = new word.application(); word.document doc = wordapp.documents.open(newfile, false); doc.activewindow.selection.wholestory(); doc.activewindow.selection.copy(); string text = doc.content.text; doc.close(ref nullobj, ref nullobj, ref nullobj); wordapp.quit(ref nullobj, ref nullobj, ref nullobj); file.delete(newfile); return text; } private void lvresults_doubleclick(object sender, eventargs e) { process.start(lvresults.selecteditems[0].text); lvresults.selecteditems[0].forecolor = color.purple; } private void btnbrowse_click(object sender, eventargs e) { folderbrowserdialog fd = new folderbrowserdialog(); if (fd.showdialog() == dialogresult.ok) { tbdirectory.text = fd.selectedpath; btnsearch.enabled = true; } } } }
thanks in advance!
[dllimport("kernel32.dll", charset = charset.unicode, callingconvention = callingconvention.stdcall, setlasterror = true)] [return: marshalas(unmanagedtype.bool)] static extern bool copyfile( [marshalas(unmanagedtype.lpstr)] string lpexistingfilename, [marshalas(unmanagedtype.lpstr)] string lpnewfilename, [marshalas(unmanagedtype.bool)] bool bfailifexists);
in dllimport
declaration select charset.unicode
character set. mean p/invoke function bound copyfilew
.
but subsequently instruct marshaller marshal parameters lpstr
, ansi strings. reason why function fails.
a correct p/invoke be:
[dllimport("kernel32.dll", charset = charset.unicode, setlasterror = true)] static extern bool copyfile(string lpexistingfilename, string lpnewfilename, bool bfailifexists);
you absolutely should not p/invoke getlasterror
. instead use marshal.getlastwin32error
reasons described in documentation.
Comments
Post a Comment