vb.net - vb filepath getter code gets different filepath then C# getter code -
i converting vb website app c# , of conversions producing weird results example
dim holidayfilepath = hostingenvironment.mappath("~/content/usa_holidays.txt") dim slist new list(of date) slist = getholidays(holidayfilepath) dim s string = slist.aggregate("", function(current, ddate) current & ddate.tostring())
returns filepath variable
"x:\folder\toolkit\toolkit\content\usa_holidays.txt"
and code snippet works expected, producing list of dates strings.
but c# version
public actionresult index() { var holidayfilepath = hostingenvironment.mappath("~/content/usa_holidays.txt"); var slist = new list<datetime>(); slist = daycalcs.getholidays(holidayfilepath); var s = slist.aggregate("", (current, ddate) => current + ddate.tostring(cultureinfo.currentculture)); return view(s); }
returns holidayfilepath variable
"x:\\folder\\toolkit\\toolkit\\content\\usa_holidays.txt"
which produces exception.
is there reason same code line produces different result in c#? why c# version insert double backslashes causing error?
it produces typeinitializationexception innerexception
{"could not find part of path 'x:\\folder\\toolkit\\toolkit\\datafiles\\holidays.txt'."}
isn't variable call supposed do? correct file path, finding folder datafiles? since error occurring when calling getholidays, here code snippett
public static list<datetime> getholidays(string holidaysfile) { var salldates = file.readalllines(holidaysfile); return salldates.select(convert.todatetime).tolist(); }
update:
well exception said looking datafiles/holidays.txt, absolutely 0 clue why, changed folder , filename , works. incredibly strange. text file , folder, original names created in solution using add new item dropdown exception coming beyond understanding.
in vb.net, strings more literal , there isn't need escape backslashes. in c#, backslash escape character, has escaped itself.
you should try like:
var holidayfilepath = hostingenvironment.mappath("~/content/usa_holidays.txt"); var fullpath = path.getfullpath(holidayfilepath); var slist = new list<datetime>(); slist = daycalcs.getholidays(fullpath);
Comments
Post a Comment