I'm trying to make a little test app to compress and extract folders, and here is my code :
Imports System.IO
Imports System.IO.Compression
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
FolderBrowserDialog1.ShowDialog()
If Not FolderBrowserDialog1.SelectedPath = "" Then
Dim startPath As String = FolderBrowserDialog1.SelectedPath
Dim zipPath As String = "C:\Users\LENOVO\Desktop\result.zip"
ZipFile.CreateFromDirectory(startPath, zipPath)
End If
End Sub
End Class
And the error is in the ZipFile command :
ZipFile is not declared. it may be inaccessible due to its protection level.
I'v searched a lot, and my target framework is 4.5.2 so Zip file should be supported, and i'm using visual studio 2017.
Any help will be appreciated.
I solved the problem with adding a reference, just go to Project tab --> Add Reference --> Assemblies tab and then select System.IO.Compression.FileSystem and add it, and it should work, here is some images too :
Hope someone will find this useful.
This may not affect everyone, but this is what I had to do to solve this issue (leaving this here as a self-note as much as anything).
I ended up having to find System.IO.Compression.dll, System.IO.Compression.Filesystem.DLL, and System.IO.Compression.ZipFile.dll on my computer and move them to the BIN folder of the site I'm working on. As soon as I did so, any "not declared" errors disappeared.
Similar problem as the initial question. I followed the other solutions/comments above and ended up with having to do the following which has slight variations to the details:
Add System.IO.Compression.FileSystem to the Assembly references
Add both the below imports code lines to the beginning of the code:
Imports System.IO '(Needed for additional file system functions used)
Imports System.IO.Compression.ZipFile
Calling function without preceeding "ZipFile.":
CreateFromDirectory(FileLocation1 & "\", FileLocation1 & ".zip")
I hope this may support others facing the same issue.
BR
Fnk
Related
This question may be a little stupid, but i'm curious if it is possible or not.
I have a file named library.xxx(contains vbscript code), which contains predefined functions. And i have an other file test.vbs(also contains vbscript code, just here i use the functions, which are defined in library.xxx). In test.vbs the library is "included", which means, i can use the functions from library.xxx. For example, there is a function called ProgramFiles, and if i call it in test.vbs, i will receive the Program Files folder location.
The problem is, that library.xxx is visible in this way. There is an application called ScriptCryptor. With this application, i can open my library.xxx and make an .exe of it, which would be better for me, since it is not clear text.
My problem is now, how could i execute the command which are called in test.vbs? I think i should read line by line the test.vbs file, and process it somehow. But how? How do i know if the line i read is a function or just a variable? Or both? And how to process them?
Is there some way to do that?
Hopefully it is understandable what i want.
Thanks!
By far the easiest way to accomplish this is to include "library.vbs" in to your "test.vbs" file.
For example:
Library.vbs:
Function ProgramFiles()
ProgramFiles = "C:\Foo"
End Function
test.vbs:
sub includeFile (fSpec)
dim fileSys, file, fileData
set fileSys = createObject ("Scripting.FileSystemObject")
set file = fileSys.openTextFile (fSpec)
fileData = file.readAll ()
file.close
executeGlobal fileData
set file = nothing
set fileSys = nothing
end sub
includeFile "library3.vbs"
wscript.echo ProgramFiles
Your question seems to indicate that you may already be doing this so if you are then I apologize.
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
If clear text truly is bothering you then from what I have seen there is no way to make the executable from ScryptCryptor to be made available to your vbscript.
Instead you could create a COM Library DLL to be used as an object in your test.vbs file.
The downside to this is that it will be necessary to learn a new language. Visual Studio Visual Basic certainly is different from Windows Shell Script Visual Basic but it would work for what you want.
Steps to take:
Download Visual Studio 2013 Express for Windows Desktop (or trial version of Ultimate or whatever you feel is appropriate for you)
Open Visual Studio as an Administrator
Create a new Project. Select a "Class Library" under the "Visual Basic" templates
Copy and paste the code below
<ComClass(OurLibrary.ClassId, OurLibrary.InterfaceId, OurLibrary.EventsId)>
Public Class OurLibrary
Private userNameValue As String
Public Const ClassId As String = "40491A82-D53A-46A6-B7E0-1CDF78A33AB6"
Public Const InterfaceId As String = "B49C996C-B039-471D-BF17-0DDA5B3CF517"
Public Const EventsId As String = "6245E3DD-DEB5-4B75-AC03-F4430BC18FDE"
Public Sub New()
MyBase.New()
End Sub
Public Sub mycopy(mySource As String, myDest As String)
My.Computer.FileSystem.CopyFile(mySource, myDest, True)
End Sub
End Class
Click on Project -> ClassLibrary1 Properties
Click on "Compile" and check the box for "Register for COM interop"
Click on Build -> Build Solution
You now have a library that your test.vbs can use:
Set myLib = CreateObject("ClassLibrary1.OurLibrary")
mySource = "C:\mytextfile1.txt"
myDest = "C:\mytextfile2.txt"
myLib.mycopy mySource, myDest
If your like me test.vbs needed to be called as C:\Windows\SysWOW64\cscript.exe test.vbs
For more information about creating COM classes in Visual Basic see here: Walkthrough: Creating COM Objects with Visual Basic
I currently am getting the error:
Invalid TLV Record
When running the following block of code:
Private Sub PrinttoFile_Click()
Dim crApp As CRAXDRT.Application
Dim crRep As CRAXDRT.Report
Set crApp = New CRAXDRT.Application
Set crRep = crApp.OpenReport("C:\Documents and Settings\Administrator\Desktop\DansTest\Report3.rpt")
'...code to set report parameters, login information etc...
ExportReportToPDF crRep, "C:\Testing\ReportTest.pdf", "Beds Held"
End Sub
It errors on Set crRep = line. I've made sure that all my .dlls for CR9 are registered. The report was built using CR9 Reports and saved as such. CRAXDRT is the CR9 runtime activeX control. Any ideas?
Also, this is not a deployment issue. I am working on the same machine as the coding. This error is happening while debugging the original code, not while running an executable.
The answer to this problem turned out to be that my CRAXDRT.dll file for CR9 was in the Windows System32 folder. Even though the reference in VB 6.0 was pointing to the correct .dll, something about it was wrong.
I fixed the error by copying the .dll into the folder with the rest of the CR9 files, C:\Program Files\Common Files\CrystalDecisions\2.0\bin and redirected the reference here and it solve the error.
So in short, make sure your dll file is in the correct folder with the rest of the dll files.
I have got an infuriating problem trying to compile a VB6 Outlook Add-in component following an upgrade of Office on the same PC to Office 2013 Pro Plus.
The error
Compile Error: Automation type not supported in Visual Basic
is being thrown on the line:
Implements IDExtensibility2
I will paste the full code from that file below.
On another PC, I managed to get everything set up, and upgrade Office to 2012 and it compiled no problem, but on that same PC now - I get the same error. I have messed around a lot with the Office install in the interim so I couldn't accurately tell you what I did in between it working and not working, but the bottom line is it seems it shouldn't be beyond possibility to do what I am trying to do (as I have done it!), I just need some guidance on what I may need to do to get back to a working setup. Any ideas??
Implements IDTExtensibility2
Private gBaseClass As New clsOlkAddIn
Private Sub IDTExtensibility2_OnAddInsUpdate(custom() As Variant)
'To Be Declared for IDTExtensibility2
End Sub
Private Sub IDTExtensibility2_OnBeginShutdown(custom() As Variant)
'To Be Declared for IDTExtensibility2
End Sub
Private Sub IDTExtensibility2_OnConnection(ByVal Application As Object, _
ByVal ConnectMode As AddInDesignerObjects.ext_ConnectMode, _
ByVal AddInInst As Object, custom() As Variant)
gBaseClass.InitHandler Application, AddInInst.ProgId
End Sub
Private Sub IDTExtensibility2_OnDisconnection(ByVal RemoveMode _
As AddInDesignerObjects.ext_DisconnectMode, custom() As Variant)
Dim objCB As Office.CommandBar
On Error Resume Next
'If RemoveMode = ext_dm_UserClosed Then
Set objCB = golApp.ActiveExplorer.CommandBars("Addin")
objCB.FindControl(Type:=msoControlPopup, Tag:="Menu").Delete
objCB.FindControl(Type:=msoControlButton, Tag:="AddEmailsButton").Delete
objCB.FindControl(Type:=msoControlButton, Tag:="SyncContactsButton").Delete
objCB.FindControl(Type:=msoControlButton, Tag:="SyncTasksButton").Delete
objCB.FindControl(Type:=msoControlButton, Tag:="SyncCalendarButton").Delete
objCB.Delete
'End If
gBaseClass.UnInitHandler
Set gBaseClass = Nothing
End Sub
Private Sub IDTExtensibility2_OnStartupComplete(custom() As Variant)
'To Be Declared for IDTExtensibility2
End Sub
Another cause of this error, as I found today is having an optional parameter on a function and giving it a default, e.g.
Public Function Foo(MyString as String, Optional MyVariant as variant = null)
Changing this to:
Public Function Foo(MyString as String, Optional MyVariant as variant)
Solved it, I didn't get there as simply as it looks above though (hence, why I'm hanging around here), I did change the optional parameter to a string first, which worked, in fact, even with a variant parameter, having a default of anything other than Null worked. It was the default of Null that was causing the error.
As you've confirmed in your answer (in comments), ensuring you've upgraded to the latest service pack is always the best option unless you know you're relying upon features that were dropped or changed in later service packs.
Installing the "Cumulative update rollup for the Visual Basic 6.0 Service Pack 6 Runtime Extended Files" (https://support.microsoft.com/en-us/help/957924/description-of-the-cumulative-update-rollup-for-the-visual-basic-6-0-s) resolved this error in my instance.
I'm writing an addin for Microsoft Outlook 2007. My goal is to open a work item in a Visual Studio 2010 instance, which I know exists and has the packages loaded for TFS 2012. The following code works fine, until the last line gives me an InvalidCastException:
Imports Microsoft.VisualStudio.TeamFoundation.WorkItemTracking
Imports Microsoft.TeamFoundation.Client
Imports Microsoft.VisualStudio.OLE.Interop
Imports Microsoft.VisualStudio.Shell
Private Sub Test()
Dim vsObj As IServiceProvider = System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE.10.0")
Dim sp As New ServiceProvider(vsObj)
Dim docService As DocumentService = sp.GetService(GetType(DocumentService))
... do stuff with docService ...
End Sub
Here is the full exception text:
Unable to cast COM object of type 'System.__ComObject' to class type 'Microsoft.VisualStudio.TeamFoundation.WorkItemTracking.DocumentService'. Instances of types that represent COM components cannot be cast to types that do not represent COM components; however they can be cast to interfaces as long as the underlying COM component supports QueryInterface calls for the IID of the interface.
I asked the service provider for a DocumentService, and it gave me something, but that something tells me it's not actually a DocumentService. What's the deal? How do I get the DocumentService to know it's a DocumentService, or how do I get something that actually is one?
edit: I've also tried removing "As DocumentService" to get past the invalid cast, but nothing else about the object is discoverable when I inspect it as a Watch. I also tried the following (recommended here), hoping it would shed some light on the problem:
Dim vsObj As IServiceProvider = System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE.10.0")
Dim sp As New ServiceProvider(vsObj)
Dim docService = sp.GetService(GetType(DocumentService))
'Note that I still didn't specify a type for docService above.
MsgBox(Microsoft.VisualBasic.Information.TypeName(docService))
To my horror, the message box said "DocumentService", merely reaffirming my indignation. I really have no idea what to do with that.
This related article is the closest I could find to somebody having the same problem, but it was not answered: How to open WorkItem (VS-Team Explorer) from outside visual studio?.
Thank you!
Is there any quick way to run a file(.cs) in VS 2008 with a Main method ?
Often you'd want to test some mockup code, Going Alt+f7(Project->ProjectName Properties) and changing the Startup object from a dropdown list is quite cumbersome.
Get yourself the SnippetCompiler, it's made to run snippets (not inside of VS, but close enough) and may help you.
What about instead of mockups, writing those as unit tests. You can run those quickly without changing entry points. And the tests could stick around for later changes. Instead of writing to the Console, you would use Asserts and Trace Writes.
To compile one file C# programs I have created a .bat file, on which I drag and drop a .cs file and get a .exe in .cs file directory.
SET PATH=%PATH%;C:\WINDOWS\Microsoft.NET\Framework\v3.5
cd %~d1\
cd "%~p1"
csc %1
You can use this .bat file in a Visual Studio macro to compile active .cs file and run the application.
Sub RunCS()
If Not ActiveDocument.FullName.EndsWith(".cs") Then
Return
End If
REM Path to batch file
Dim compileScript = "C:\dev\compileCS.bat"
Dim compileParams As System.Diagnostics.ProcessStartInfo
compileParams = New ProcessStartInfo(compileScript, Chr(34) & ActiveDocument.FullName & Chr(34))
Dim compiling As System.Diagnostics.Process
compiling = System.Diagnostics.Process.Start(compileParams)
compiling.WaitForExit()
Dim programFile As String
programFile = ActiveDocument.FullName.Substring(0, ActiveDocument.FullName.Length - 3) + ".exe"
Dim running As System.Diagnostics.Process
running = System.Diagnostics.Process.Start(programFile)
End Sub
This will run only programs for which all code is in one file. If you want to quickly change projects instead, you can change your solution's Startup Project to Current selection
I keep a sandbox solution around that has a console project and other tiny project types that I use frequently. Snippet Tools are nice but usually don't come with the whole Visual Studio shebang like debugging etc.
Snippy, originally by Jon Skeet (who lurks on here I believe) and further developed by Jason Haley.