how do I set folder share programmatically in vb.net - vb.net

I have Client-Server environment and developed a project for Client-Server.
I need to share a folder of my Server machine programmatically using VB.NET
Please help me.

Here's one example which shows the concept using ManagmentClass. It's C# but easily convertible to VB.NET:
UPDATE:
Directory.CreateDirectory("C:\MyTestShare")
Dim managementClass As New ManagementClass("Win32_Share")
Dim inParams As ManagementBaseObject = managementClass.GetMethodParameters("Create")
inParams.Item("Description") = "My Files Share"
inParams.Item("Name") = "My Files Share"
inParams.Item("Path") = "C:\MyTestShare"
inParams.Item("Type") = 0
If (DirectCast(managementClass.InvokeMethod("Create", inParams, Nothing).Properties.Item("ReturnValue").Value, UInt32) <> 0) Then
Throw New Exception("Unable to share directory.")
End If

I have code that looks similar to this which works on vista and win2k3 machines, but when i try it on Windows server 2008 R2 (with recent updates) it fails with an "access denied" error. I've tried your exact code above and the same result. I am an Admin on the box and I have tried disabling UAC but without any effect.
(i know this isn't an answer, I have no power to comment)

Related

VBNet changing Active Directory passwords of expired or one time password accounts

I need to change the Passwords of an Active Directory account in windows 10 in VB.NET.
The program, I wrote, runs as local administrator,
My working code with a valid user account is (Domain_xps and UserName_xps are Strings and pwdPtr System.Runtime.InteropServices.Marshal.SecureStringToBSTR of a SecureString):
dEntry = New DirectoryServices.DirectoryEntry("LDAP://" & Domain_xps, UserName_xps
, System.Runtime.InteropServices.Marshal.PtrToStringBSTR(pwdPtr)
, System.DirectoryServices.AuthenticationTypes.Secure
+ System.DirectoryServices.AuthenticationTypes.Sealing
+ System.DirectoryServices.AuthenticationTypes.ServerBind) ',pwd)
nativeObject = dEntry.NativeObject
Dim searcher_Fullname_xpo As System.DirectoryServices.DirectorySearcher
= New System.DirectoryServices.DirectorySearcher(dEntry)
With searcher_Fullname_xpo
.Filter = "(&(objectClass=User) (sAMAccountName=" & UserName_xps & "))"
End With
result_xpo = searcher_Fullname_xpo.FindOne
Dim user As DirectoryServices.DirectoryEntry 'open directory
user = result_xpo.GetDirectoryEntry() 'get directory results
user.Username = UserName_xps
user.Password = PWD_xps
user.Path = result_xpo.GetDirectoryEntry().Path
user.AuthenticationType = System.DirectoryServices.AuthenticationTypes.Secure
+ System.DirectoryServices.AuthenticationTypes.Sealing
+ System.DirectoryServices.AuthenticationTypes.ServerBind
user.Options.PasswordPort = 389
user.Options.PasswordEncoding = 1
user.Invoke("ChangePassword", New Object() {PWD_xps, PWDNeu_xps})
user.CommitChanges() 'commit changes
user.Close() 'close directory
But if a account has expired through holidays or if a new user with a one time password is generated and tries to change his password, i get an error.
The user or password are wrong.
while debugging i noticed, that following lines produce the same error.
nativeObject = dEntry.NativeObject
Dim searcher_Fullname_xpo As System.DirectoryServices.DirectorySearcher = New System.DirectoryServices.DirectorySearcher(dEntry)
With searcher_Fullname_xpo
.Filter = "(&(objectClass=User) (sAMAccountName=" & UserName_xps & "))"
End With
result_xpo = searcher_Fullname_xpo.FindOne
And also
user.Options.PasswordPort = 389
user.Options.PasswordEncoding = 1
user.Invoke("ChangePassword", New Object() {PWD_xps, PWDNeu_xps})
each of these produce the error and doesn't change the password correctly, after encapsulating every line in a try catch expression.
Active Directory shows a change, but the user account is not valid any more.
I tried basically the same methods, in hope that i could set the options.
Dim ADS_OPTION_PASSWORD_PORTNUMBER As Long = 6
Dim ADS_OPTION_PASSWORD_METHOD As Long = 7
Dim ADS_PASSWORD_ENCODE_REQUIRE_SSL As Integer = 0
Dim ADS_PASSWORD_ENCODE_CLEAR As Integer = 1
Try
user.Invoke("SetOption", New Object() {ADS_OPTION_PASSWORD_PORTNUMBER, 389})
Catch ex3 As Exception
End Try
Try
user.Invoke("SetOption", New Object() {ADS_OPTION_PASSWORD_METHOD, ADS_PASSWORD_ENCODE_CLEAR})
Catch ex3 As Exception
End Try
And i tried also
user.Invoke("SetPassword", New Object() {PWDNeu_xps})
the error message stays the same
To set the password seems the right way, but as i can't set the password port or enable the password method, it produces the same error.
i also found this old thread How to change password in active directory when password expired
but that is not longer possible under windows 10.
How can i change the password, of an expired account with the needed options in VB Net or can i configure the account, so that it is possible to achieve it.
So an update on this situation. 28.10.2021
I also tried as workaround a powershell command as described in Microsoft or here for that matter.
And the same thing happens, the password change isn't possible,
Set-ADAccountPassword : The server has rejected the client credentials.
With a valid user the comand works as does it in DotNet
Write a program in a 32 bit version of C or up to C++11 on a 32 bit version of Microsoft Windows up to XP pro sp2 (and not later version) which does what you want to do. Compile it with Code::Blocks up to version 17.12 and not a later version. Do not allow any version of Visual C++ or Visual Basic or Visual Studio on that computer before or during this process.
Load and run that program, on your Windows 10 computer, as the highest administrator level that Windows 10 will "allow". If you cannot get it to run compiled as GUI, then try to get it to run compiled as CLI. Remember, This is Extremely important: Compile the program as a stand-alone single executable that uses NO external dlls.
Change the password outside of any Windows 10 commands. Edit the file that they are in with your program, and do not tell Windows 10 anything is happening.

Error Printing: No application is associated with the specified file for this operation

I have a VB WinForms app that prints PDFs using Process.Start, and it has been working fine for ages on Windows 10. Today I go to use it and get the following error message:
"No application is associated with the specified file for this operation"
Nothing has changed to cause this, I have not changed PDF Viewer or uninstalled anything. I can open a PDF by double clicking on it no problems.
I wrote a small console app to replicate the issue and prove this had nothing to do with the WinForms app:
Sub Main()
Dim pi As New Diagnostics.ProcessStartInfo
Dim url As String = "C:\PathToPDF\.pdf"
pi.FileName = url
pi.Verb = "PrintTo"
pi.CreateNoWindow = True
pi.Arguments = """Microsoft Print to PDF"""
pi.UseShellExecute = True
Console.WriteLine(url)
Diagnostics.Process.Start(pi)
Console.ReadKey()
End Sub
The above program replicates the error. I have verified that the file exists and is accessible and tried it with double slashes and single slashes, they give the same error:
"No application is associated with the specified file for this operation".
The same application is working fine on other PCs in the office.
Has anyone else had this happen, and if so how did they fix it?
My PDF viewer is PDFXChange Viewer, and it has been working fine PDFs printed using this method for years. I have not updated the program, and it says in the about page that the last installed updated was in 2016.
Regards.
If I am right, I had the same issue. Maybe you can try it in a slightly other way. I have this in my application and it works fine:
Dim myp As New Process
myp.StartInfo.FileName = filename 'Full path to pdf
myp.Start()

Crystal Reports failed to open the connection

I am developing a desktop software which is creating proposals and invoices. I am storing my database (MySql) in a remote server. Everything is working perfectly fine in my PC which i am using for coding.
On client machine I can add,update and delete records without any problem. But when i try to open a record in then I am facing with an error below.
I was created the report in VB.NET but when I face with the error than I opened the rpt file in Crystal Reports XI Release 2 and updated data source location, verified database and save the file. Some part of my code is :
ElseIf Me.Text = "Tekliflere Gözat" Then
Form16.Text = "Teklif Detayları:"
Dim strReportPath As String
Dim rptDoc As CrystalDecisions.CrystalReports.Engine.ReportDocument
strReportPath = My.Application.Info.DirectoryPath & "\Teklif.rpt"
rptDoc = New ReportDocument
rptDoc.Load(strReportPath)
rptDoc.SetDatabaseLogon("USER_NAME", "PASSWORD", "SERVER_IP", "DB_NAME")
Form16.CrystalReportViewer1.ReportSource = rptDoc
Form16.CrystalReportViewer1.SelectionFormula = "{teklif1.teklifno} =" & ListView1.SelectedItems(0).Text
Form16.CrystalReportViewer1.Refresh()
Form16.CrystalReportViewer1.RefreshReport()
Form16.Show()
Me.Close()
Could you please advise.
Thank you.
Regards
Oguz
I've found the solution. But first have to look at the casue. While connecting to a remote database via my software everthing is working fine. But when I was designing a report in CR, I created a connection in database expert menu. So that means locally I am describing to CR that where and how to connect. But when I install my software with all necessery components to the client machine CR report files searching the connection criteria and as a matter of course it can not find it in client machine.
So the solution is to install Mysql ODBC Connector to the client machine and configure it with the same settings in my PC. Or as an alternative solution, define all connection and database settings programatically in your code.
Hope this helps.

How to improve my auto-update functionn? VB.net

I'm still learning VB.net and I'm now at a stage where I want to write an auto-update function, now I wrote this simple function myself, nothing fancy but I'd just like to check to see that there's no major flaws in my logic? Short of human error I think this is a nice simple way to do this.
Note: it all works flawlessly from my testing.
My Function
Public Function updateCheck()
Dim CurrentVersion As String = My.Settings.currentVersion
Dim updateURL As String = My.Settings.updateURL
Dim WebRequest As WebClient = New WebClient
Dim Version As String = WebRequest.DownloadString(updateURL)
If Version = CurrentVersion Then
MessageBox.Show("no updates available")
Else
MessageBox.Show("An new version is available: " & Version)
End If
End Function
updatecheck.html file simply contains "vx.x.x" which sites on a web-server and the currentVersion string is again "vx.x.x"
I can't see this failing short of forgetting to change the currentVersion string upon an application update and it looping.
In terms of simplistic and clean code, is there anyway I can improve this? - I plan on adding some download and execute code to download an updater which un-installs and re-installs the latest version. - I'm currently using InstallShield to deploy the application.
Thanks for any suggestions/comments.
Instead of trying to code this yourself - you should have a look at ClickOnce deployment.
This has all the functionality you are trying to code and handles all the error cases when there is no connection, etc. It also allows for install without admin rights.

Download file in VB.NET 2010

I have looked almost everywhere on the internet and I cannot find a way to download a file from the internet into a specific folder that works with VB.NET 2010. I would like to download a file called, for instance, example.txt, and download it into, for example, %HOMEDRIVE%%HOMEPATH%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup so that it will run automatically at system startup. All help is appreciated
Guessing something based on...
Using webClient = New WebClient()
Dim bytes = webClient.DownloadData("http://www.google.com")
File.WriteAllBytes(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "MyFileName.ext"), bytes)
End Using
As for the startup, VB.NET has a pretty ease way to add Registry keys...
My.Computer.Registry.SetValue
To set something like HKEY_CURRENT_USER\Software\Microsoft\CurrentVersion\Run
UPDATE
How to: Create a Registry Key and Set Its Values in Visual Basic
http://msdn.microsoft.com/en-us/library/cy6azwf7(v=VS.100).aspx
I would suggest using WebClient.DownloadFile. Use Environment.SpecialFolder.Startup to get the path to save the file.
Sub Main()
Using wc As New WebClient()
Dim startupPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup)
wc.DownloadFile("http://MyDomain.com/MyFile.txt", Path.Combine(startupPath, "test.txt"))
End Using
End Sub