I use the Directory.getLastWriteTimeUtc() method. If the path is too long for this method it throws an PathTooLongException. So I started to use Google finding a solution for this problem. A couple of search results adviced me to use \\?\ as a prefix. Using the Windows Explorer this solution works perfectly. But it does not work with Visual Basic. Adding this prefix to the path the result is an ArgumentException because the is an illegal character in the path.
So I want to ask you if you have a solution to handle long paths with Visual Basic.
The snippet of code that causes the problem is the following one. It is used to get the most recent file of a folder.
Oh, I'm sorry. Of course I will show you my snippet of code that causes the problem.
Try
If Directory.GetLastWriteTimeUtc(s) > Directory.GetLastWriteTimeUtc(latest) Then
latest = s
End If
Catch e As PathTooLongException
Console.WriteLine("error...")
End Try
I solved the problem by subsitute the Delimon.Win32.IO for the System.IO library. Now I can handle such long paths. Hope I can help anyone later.
Related
I have an error with some code that I have been working on for a while. I am writing a speech recognition software in Visual Basic.
grammar.Append(New Choices(System.IO.File.ReadAllLines(My.Resources.Commands)))
The above line has illegal characters in path in the InnerException. however if the line of code bellow is in place instead of the one above it will run fine.
C:\Users\oem\documents\visual studio 2010\Projects\VC\VC\CustomCommmands.txt
This will now work if the application is compiled into a exe setup and ran on a different machine.
I have looked all over Google for a answer, but none seem to work. If anyone can shed some light, it will be much appreciated.
System.IO.File.ReadAllLines is only used when you are reading a file from disk. It sounds like you have embedded the file as a resource in your program.
I don't have vb.net to test this, but this will probably work... I believe My.Resources loads the resource values directly, so you don't have to load them at all.
grammar.Append(New Choices(My.Resources.Commands))
I'm trying to recover lost code from a VB.net 2.0 application that I've inherited. I have current working versions, so I've tried using RedGate Reflector and Telerik JustDecompile to recover the code. Both will successfully return code files that are readable and Visual Studio project files, but when I try to run the decompiled program, I get 102 error messages (with both decompilers).
I have working Visual Studio projects from old versions, which run fine, but the structure of the solution and code files is completely different and I'm afraid the program was changed too much since then to trust these old versions.
The only odd assembly that was included is SmartCodeDeveloper 1.0.2986.26049. And I suspect the code was originally written in Visual Studio 2005, as both 2008 and 2012 want to upgrade when the solution is opened.
What else can I do to recover this program? I have the code, but something else is preventing it from running, and I'm at a deadend.
EDIT:
Some of the error messages I'm getting are:
-"End of Statement Expected" or "Statement cannot appear within an event body. End of Event assumed."
-Many are variations on "RemoveHandler" definition missing for object.
-object not declared "It may be inaccessible due to its protection level."
EDIT 2:
Is there any advantage to trying to decompile into C#? Is it possible Reflector may do a better job converting to that? What about different .NET framework versions? I've been doing 2.0, which I believe it was developed on.
I've seen this same problem once before. Described by another member.
Its really simple to correct.
The reason you have these 100+ errors is usually because of something trivial...
Like an unterminated strig for example.
Yet a simple error pushes everything else out of sync so you get more errors displayed than the actual amount of REAL errors, because of the ladder effect that ocurs due to the first error (cause of issue).
The BEST way to deal with it is as follows....
1 - Open your solution
2 - Close all document windows - so no solution documents are displayed
3 - Compile your solution
4 - At this point your errors should be minimised to a much lower amount of errors like maybe 1 to 10 for example.
Try that and let me know how many errors are listed AFTER you have done the above.
Reflector will create code that isn't VB code, ie. var++ . You will have to find them and fix them. It will also create many event definitions that won't work. Look for code Like Public Custom Event definitions. Delete the Custom keyword and all the lines following until End Event. Also you will need to figure out what Import statements you need.
Here is an example from Reflector
Public Custom Event ColumnChanging As DataColumnChangeEventHandler
AddHandler(ByVal value As DataColumnChangeEventHandler)
Bid.Trace("<ds.DataTable.add_ColumnChanging|API> %d#" & ChrW(10), Me.ObjectID)
Me.onColumnChangingDelegate = DirectCast(Delegate.Combine(Me.onColumnChangingDelegate, value), DataColumnChangeEventHandler)
End AddHandler
RemoveHandler(ByVal value As DataColumnChangeEventHandler)
Bid.Trace("<ds.DataTable.remove_ColumnChanging|API> %d#" & ChrW(10), Me.ObjectID)
Me.onColumnChangingDelegate = DirectCast(Delegate.Remove(Me.onColumnChangingDelegate, value), DataColumnChangeEventHandler)
End RemoveHandler
End Event
It would be changed to this :
Public Event ColumnChanging As DataColumnChangeEventHandler
Missing References will cause many errors also.
I am trying to compile a piece of Code at Runtime with the VBCodeProvider class.
It works great with one exception. I can't access any members of the System.Drawing
Namespace.
I do an "Imports System.Drawing" at the beginning of the code which does
compile without an error (so I guess the reference is there alright) but when I try
to define for example a graphics object with "Dim g as Graphics" or "Dim g as System.Drawing.Graphics" I get an exception on compilation: {[...] error BC30002: Der Typ "Graphics" ist nicht definiert. (The type Graphics is not defined)}
When I try to add the System.Drawing.dll to the ReferencedAssemblies of the CompilerOptions then I get a whole bunch of other compilation errors so I guess that's not the solution.
I don't really get why this problem is there. For example in the same code I use members of System.Math which work.
Does anyone have an idea or tip for me of what I may be doing wrong?
Thank you in advance.
Jens
edit due to lack of reputation:
Thank you for your help. Typical thing: You try and search for over an hour, then you ask here and 10 minutes later you find the solution yourself:
The problem was with how I referenced the system.drawing.dll in the compileroptions. I did use an absolute path "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.0\System.Drawing.dll" and that gave me 19 exceptions in some code I did not even write myself.!
What worked was to use a relative path. e.g.
PM.ReferencedAssemblies.Add("System.Drawing.dll")
I don't know what the real difference is, but at least this works. :-)
Thank you very much for your help!
Typical thing: You try and search for over an hour, then you ask here and 10 minutes later you find the solution yourself:
The problem was with how I referenced the system.drawing.dll in the compileroptions. I did use an absolute path C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Drawing.dll and that gave me 19 exceptions in some code I did not even write myself.
What worked was to use a relative path. e.g.
PM.ReferencedAssemblies.Add("System.Drawing.dll")
I don't know what the real difference is, but at least this works.
I'm seeing a strange build bug a lot. Sometimes after typing some code we receive the following build error.
Class 'clsX' must implement 'Event PropertyChanged(sender As Object, e As PropertyChangedEventArgs)' for interface System.ComponentModel.INotifyPropertyChanged'.
And
'PropertyChanged' cannot implement 'PropertyChanged' because there is no matching event on interface 'System.ComponentModel.INotifyPropertyChanged'.
Those error should never go together! Usually we can just ignore the exception and build the solution but often enough this bug stops our build. (this happens a lot using Edit and Continue which is annoying)
We're using Vb.net and c# mixed in one big solution.
Removing the PropertyChanged event and retyping the same code! sometimes fixes this.
Question:
Has anyone else seen this problem and has some suggestions how to prevent his?
We're using a code generator that causes this error to surface but just editing some files manually triggers this exception too. This error occur's on multiple machines using various setups.
Someone had the same exact issue discussed here. It sounds like there is an issue with this build picking up an old version of a binary. I would try the following in order:
Verify all assembly references use project references where possible within the Visual Studio solution.
Disable build parallelization in case there is some weird file locking issue with concurrent project builds. Go to Tools -> Options, Projects and Solutions -> Build and Run, then set "maximum number of parrellel project builds" to 1. Not the best solution but it may help narrow down the problem.
Disable the Hosting Process in case it's locking some file causing an assembly to not get rebuilt correctly. For C# project go to Project Properties, Debug tab, and uncheck "Enable the Visual Studio hosting process". For VB.NET project you'll need to Unload Project, Edit the project file, and add <UseVSHostingProcess>false</UseVSHostingProcess> to the PropertyGroup of each configuration. Again, not the best solution but you probably won't notice a difference.
Lastly, try doing a Clean + Build to try and resolve the issue when it occurs (I know this is not a fix but it's easy enough to do), also Rebuild may be slightly different than Clean + Build so try the latter if the former doesn't work.
As I can not comment due to lack of appropriate points.
But I would like to share one of my experience:
In an aspx.cs page I was working, used to compile fine and some time gave mysterious error of a variable not defined or function not defined or sometime variable or the function defined two times. I changed possibly each and every variable and function name but there seemed no effect , but after entering a simple space or a new line at any place in the file used to solve the compile error. At one time I tried to save the file (in a different encoding as i am used to experiments) and found that the file was not saving in the correct encoding (i.e. the ansi encoding because the file had a unicode character ), I removed the unicode character and that compile error didn't bothered me again.
This unicode character problem could be (not a hard and fast rule) there so you could check it.
Nuke & restore using source control (TFS instructions here):
Make sure you have everything checked in
Exit Visual Studio
Rename the project directory to .Bak (effectively deleting it)
Reopen Visual Studio and in source control:
Get Specific Version
check 'Overwrite... not checked out' and 'Overwrite ... even if local version matches'
Re-open project
Another problem: Make sure some source files are not newer than the current date (or your date is set back). Often this happens in apps where you are doing logic that requires certain things to happen differently on certain dates. You change your clock to test it, make a revision to the source with the date advanced, set the date back, and viola, rebuild does not rebuild that file.
You say 'typing it in again' - can you try just saving? After 40 years since MULTIX the .net build still decides what has changed by checking the file timestamp.
good luck!
When you get the error, is it always on the VB calling C# side, or vice-versa, or does it work both ways?
If the answer is either of the first two situations, try building the "callee" project within the solution before building the "caller" project to see if it stops the situation.
Also, just in case it may jog something for you to think about, does this error crop up when you change a VB file or a C# file, or is there no correllation?
Oh, and sorry this looks like an answer instead of a comment, I cannot post comments yet (need 50 rep).
I am struggling with this program which uses emgucv(an opencv wrapper for .net) for about 2 weeks. The problem is unfortunately not programming, but setting up emgucv in such a way that it works. I didn't manage to do so for vb.net so I tried doing it for ironpython(because I know python too). Emgucv seems to work perfectly when using ironpython, so I created a function that takes an image as an argument and analyses it in the way I want, returning another image with the results in it. The problem is I want to call this function, giving it the image argument(it could be a string containing the path) from within VB.net and become another string containing the result image as return. I later plan to package that project in a setup so I can redistribute it.
So I am asking you guys: Do you know an easy way to call an IronPython function in VB.net in such a way so I can also package the whole project and redistribute it to people?
Thank you so much for reading this and it would be great if you could also help me with my problem! :)
While IronPython is not my expertise I am well versed in EMGU and its applications. If you insist in using IronPython the following website clearly shows how to pass a string to an IronPython Class.
The following code is taken from the link and is not my own:
Option Explicit On
Option Strict On
Imports Microsoft.Scripting.Hosting
Imports IronPython.Hosting
Imports IronPython.Runtime.Types
Module Module1
Sub Main()
Dim helloWorld As New HelloWorldVB()
Console.WriteLine(helloWorld.HelloWorld("Maurice"))
Dim runtime As ScriptRuntime = PythonEngine.CurrentEngine.Runtime
Dim scope As ScriptScope = runtime.ExecuteFile("HelloWorld.py")
Dim pythonType As PythonType = scope.GetVariable(Of PythonType)("HelloWorldIronPython")
helloWorld = CType(runtime.Operations.Call(pythonType), HelloWorldVB)
Console.WriteLine(helloWorld.HelloWorld("Maurice"))
Console.ReadLine()
End Sub
End Module
I would follow the tutorial from the link but the important code is bellow as this imports the require runtime information for IronPython:
**Imports Microsoft.Scripting.Hosting
Imports IronPython.Hosting
Imports IronPython.Runtime.Types**
&
**Dim runtime As ScriptRuntime = PythonEngine.CurrentEngine.Runtime
Dim scope As ScriptScope = runtime.ExecuteFile("HelloWorld.py")
Dim pythonType As PythonType = scope.GetVariable(Of PythonType)("HelloWorldIronPython")
helloWorld = CType(runtime.Operations.Call(pythonType), HelloWorldVB)
Console.WriteLine(helloWorld.HelloWorld("Maurice"))**
Obviously Console.WriteLine(helloWorld.HelloWorld("Maurice")) would be corrected to:
Dim result_location As String = helloWorld.HelloWorld("Maurice")
Where "Maurice" would be the string containing your image location.
Now I have to ask about the problems you were having setting up EMGU in visual studio I know it can be frustrating to do especially to people who are new to it. If you would like I would be happy to help you set it up properly. The reason I ask is since you are providing this to and end user your code could be more efficient without calling IronPython. Especially since each process will require reading and writing from the hard drive.
To start: I will assume that you have included References to Emgu.CV, Emgu.CV.UI, and EMGU.Util in your project. But it is essential that you add "opencv_core220.dll", "opencv_imgproc220.dll" files directly to your project and ensure in the properties window that the 'Copy to Output' option is set to "Copy always". If it isn't you will get errors of not a having the image in the right format etc. You only really need these two .dll to read images in and access the data etc you may need others for .avi movies for example. Note that these two .dll must now be distributed with your project for it to work.
To note this may change depending if your using a 64 bit machine or a 32 bit machine but the 64 bit EMGU version will not run on X86 machines. You must also ensure that your target platform is correct in Visual Studio.
I hope this helps you,
Cheers
Chris