vb.net - WPF - URI relative to currentdirectory? -
i'm converting windows forms vb project wpf 1 (a diary program). in windows forms version, displayed images in picturebox, , pass either full path or path relative my.computer.filesystem.currentdirectory - picturebox work out.
in new wpf version, i'd same. however, works absolute, not relative paths:
dim thebitmap new bitmapimage thebitmap.begininit() thebitmap.urisource = new uri(theurl, urikind.relativeorabsolute) thebitmap.endinit() theimage.source = thebitmap just confirm, images aren't resources, aren't available @ design time, can't copy them solution, , don't want locate them in executable's directory (uac won't let me without running admin anyway guess)
everything i've read here , elsewhere wpf uris seems suggest can relative executable directory, or working directory, seems can set in project->debug page @ design time - not want.
can wpf consider uri relative currentdirectory, or have write own test it?
many thanks
nick
edit
by way, test using in meantime is:
if io.file.exists(databaselocation & "\" & theurl) 'must relative url thebitmap.urisource = new uri(databaselocation & "\" & theurl,urikind.absolute) else 'must absolute url thebitmap.urisource = new uri(theurl, urikind.absolute) end if
you should able convert path relative current directory absolute path doing following:
path.combine(directory.getcurrentdirectory(), relativepath) then use resulting path create absolute uri can use.
edit: given requirement incoming path either absolute path or path relative current directory, suggest writing utility helper method such following:
/// <summary> /// given file system path, return absolute uri representing path. /// </summary> /// <param name="path">the file system path turn uri</param> /// <returns> /// if provided path absolute, returns absolute uri representing path. /// if provided path relative, returns absolute uri representing path relative current directory. /// </returns> /// <exception cref="argumentnullexception">the provided path value null</exception> /// <exception cref="argumentexception">the provided path value empty string or invalid file system path</exception> public static uri getabsoluteuri(string path) { if (path == null) throw new argumentnullexception("path"); if (path == string.empty) throw new argumentexception("path cannot empty string.", "path"); uri uri; if (uri.trycreate(path, urikind.absolute, out uri)) { return uri; } if (uri.trycreate(path, urikind.relative, out uri)) { uri baseuri = new uri(directory.getcurrentdirectory(), urikind.absolute); uri newuri; if (uri.trycreate(baseuri, uri, out newuri)) { return newuri; } } throw new argumentexception("the provided path not converted uri.", "path"); } you give of paths method , absolute uri should able use. written, throw exception if pass invalid path, can customize want.
edit: forgot question vb , not c#. have attempted convert method vb. forgive me if not right not have experience vb.
public shared function getabsoluteuri(path string) uri if (path nothing) throw new argumentnullexception("path") end if if (path = string.empty) throw new argumentexception("path cannot empty string.", "path") end if dim inuri uri = nothing if (uri.trycreate(path, urikind.absolute, inuri)) return inuri end if if (uri.trycreate(path, urikind.relative, inuri)) dim baseuri uri = new uri(directory.getcurrentdirectory(), urikind.absolute) dim newuri uri = nothing if (uri.trycreate(baseuri, inuri, newuri)) return newuri end if end if throw new argumentexception("the provided path not converted uri.", "path") end function
Comments
Post a Comment