Prevent file from being deleted but still be able to read it - vb.net

I have searched this a lot but still I couldn't find something useful basically I tried changing file permissions to deny the delete option and keep modification and so many other combinations.I want something to prevent file from being deleted but still allow me to read it I did this both manually and using vb.net this is the code I used(it is not mine btw)
lets say I have a text file already created I want that file to be read but not deleted by the user
Imports System.IO
Imports System.Security.AccessControl
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim filename As String = "C:\users\mbpc\desktop\test.txt"
AddFileSecurity(filename, "Everyone", FileSystemRights.Delete, AccessControlType.Deny)
End Sub
Sub AddFileSecurity(ByVal fileName As String, ByVal account As String, _
ByVal rights As FileSystemRights, ByVal controlType As AccessControlType)
Dim fSecurity As FileSecurity = File.GetAccessControl(fileName)
Dim accessRule As FileSystemAccessRule = _
New FileSystemAccessRule(account, rights, controlType)
fSecurity.AddAccessRule(accessRule)
File.SetAccessControl(fileName, fSecurity)
End Sub
Sub RemoveFileSecurity(ByVal fileName As String, ByVal account As String, _
ByVal rights As FileSystemRights, ByVal controlType As AccessControlType)
Dim fSecurity As FileSecurity = File.GetAccessControl(fileName)
fSecurity.RemoveAccessRule(New FileSystemAccessRule(account, _
rights, controlType))
File.SetAccessControl(fileName, fSecurity)
End Sub
End Class

Related

file deletion exception on vb.net

My VB.NET junk cleaner cannot delete aria-Debug-10144.log since it is used by another process.
I tried this:
Imports System.Collections.ObjectModel
Imports System.IO
Public Class Form1
Dim TempDirs As ReadOnlyCollection(Of String)
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
TempDirs = (My.Computer.FileSystem.GetDirectories("C:\Users\Aitor\AppData\Local\Temp"))
Dim ListDirs As List(Of String) = TempDirs.ToList
Dim directoryName As String = "C:\Users\Aitor\AppData\Local\Temp"
For Each deleteFile In Directory.GetFiles(directoryName, "*.*", SearchOption.TopDirectoryOnly)
If Not deleteFile.ToString = "aria-Debug-10144.log" Then
File.Delete(deleteFile)
End If
Next
MsgBox("Clean completed!", MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "Results")
End Sub
End Class
But it still tries to do it.
Can anybody help me?
There are two reasons:
Firstly, the selected file is being occupied by another program. In this case, you need to determine whether the selected file is occupied before deleting it. Here is a small example.
Imports System.Runtime.InteropServices
Public Class Form1
<DllImport("kernel32.dll")>
Public Shared Function _lopen(ByVal lpPathName As String, ByVal iReadWrite As Integer) As IntPtr
End Function
<DllImport("kernel32.dll")>
Public Shared Function CloseHandle(ByVal hObject As IntPtr) As Boolean
End Function
Public Const OF_READWRITE As Integer = 2
Public Const OF_SHARE_DENY_NONE As Integer = &H40
Public Shared ReadOnly HFILE_ERROR As IntPtr = New IntPtr(-1)
Public Shared Function IsFileOccupied(ByVal filePath As String) As Boolean
Dim vHandle As IntPtr = _lopen(filePath, OF_READWRITE Or OF_SHARE_DENY_NONE)
CloseHandle(vHandle)
Return If(vHandle = HFILE_ERROR, True, False)
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If (IsFileOccupied("C:\Users\juliex\AppData\Local\Temp\aria-debug-13836.log")) Then
MessageBox.Show("File is already occupied")
Else
MessageBox.Show("File is not occupied")
'Then do some delete operation.
End If
End Sub
End Class
Secondly, the selected file is a temporary file and is currently locked. If you want to clear the temporary files it owns, you need to have full control to unlock these files and then delete them!
You should be very careful if you want to delete the TEMP file, whether it is owned by the application or by another owner. The original application may have applied the lock because it wanted to use the file!
At this time you can refer to this link below:
1.How can I unlock a file that is locked by a process in .NET
2.https://social.msdn.microsoft.com/Forums/vstudio/en-US/9e2044c5-ae5d-4552-a335-01cc567dfc58/how-to-unlock-a-file-used-by-other-process?forum=csharpgeneral

VB.NET: Can't load audio from resources

I'm trying to add multiple sounds on an application using a Public function... When I use the absolute path for my sounds everything works perfectly, but when I'm trying to load them from Resources I don't get any sound output.. Any ideas what's wrong?
Public Class Form1
Public Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Integer, ByVal hwndCallback As Integer) As Integer
Dim musicAlias As String = "myAudio"
'Dim musicPath As String = "C:\Users\Teo\Documents\Visual Studio 2015\Projects\test\test\Resources\sound.mp3"
'Dim musicPath As String = "Resources\sound.mp3"
'Dim musicPath As String = My.Resources.ResourceManager.GetObject("sound.mp3")
Dim musicPath2 As String = "C:\Users\Teo\Desktop\sound.mp3"
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
mciSendString("Open " & musicPath & " alias " & musicAlias, CStr(0), 0, 0)
mciSendString("play " & musicAlias, CStr(0), 0, 0)
End Sub
End Class
The last one works perfectly, I tried every one of the above... The three comments above are different ways I tried to make it work, but all failed...
You can load the file from resources and cache them locally and play them.
Open Resources.resx file under My Project. Then add your file for example YourFileName.mp3 to the resources by choosing Add Existing File... then when you want to play the file, use this code:
Dim file As String = System.IO.Path.Combine(Application.StartupPath, "YourFileName.mp3")
If (Not System.IO.File.Exists(file)) Then
System.IO.File.WriteAllBytes(file, My.Resources.YourFileName)
End If
'Now the file exists locally
'Play the file here
I know this has an answer marked correct, but once you have a sound file in your Resources, it's much easier this way:
My.Computer.Audio.Play(My.Resources.MyCoolSound, AudioPlayMode.Background)
No reason to use an API call.

When ever I change an attribute of a class, the window won't open

When ever I try to change an attribute in the load method of a class I created, it won't let me open it. If I leave the load method blank or put anything else there, it works fine.
Public Class Main
'SHIPS
Dim AirCraftCarrier As Ship
Private Sub Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
AirCraftCarrier.name = "ACC" ' These won't work
AirCraftCarrier.SetAttributes("ACC", AirCraftCarrier_image, 5, "vertical", new_pos, False, False) ' If I leave it blank or keep anything else here, it opens fine.
End Sub
End Class
Public Class Ship
Public name As String
Public image As PictureBox
Public direction As String
Public selection As Boolean
Public placed As Boolean
Public location(2) As Integer
Public Sub SetAttributes(ByVal name1 As String, ByVal image1 As PictureBox, ByVal length1 As Integer, ByVal direction1 As String, ByVal location1 As Array, ByVal selected1 As Boolean, ByVal placed1 As Boolean)
name = name1
image = image1
direction = direction1
selection = selected1
placed = placed1
location(0) = location1(0)
location(1) = location1(1)
End Sub
End Class
First changes to Ship. Classes tend to use Properties rather than fields:
Public Class Ship
Public Property name As String
Public Property image As PictureBox ' bad name; Net has an Image class
' etc
' set essential props via the constructor:
Public Sub New(sName As String, picB As PictureBox)
name = sName
image = picB
End Sub
Then in main for creating it:
Public Class Main
Dim AirCraftCarrier As Ship ' this is just a variable declaration
Private Sub Main_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
' Create an instance:
AirCraftCarrier = New Ship("ACC", frm.PicBoxName)
' set other properties
AirCraftCarrier.Direction = "SSW"
AirCraftCarrier.Foo = "Bar"
End Sub
End Class
With a constructor you can pass the essential information, like the unique name to the class when you create it. This is used instead of the SetAttributes sub.

Open INI file and place content in different textboxes

I am creating a program in VB.net that opens a .INI file and reads the content. I wrote the following code:
Private Sub OpenINIButton_Click(sender As Object, e As EventArgs) Handles OpenINIButton.Click
Dim OpenDLG As New OpenFileDialog
Dim FileLocation = OpenDLG.FileName.ToString()
OpenDLG.Filter = "INI File (*.ini)|*.ini"
OpenDLG.Title = "Open INI File"
OpenDLG.InitialDirectory = "C:\"
OpenDLG.RestoreDirectory = True
DialogResult = OpenDLG.ShowDialog
If DialogResult = Windows.Forms.DialogResult.OK Then
TextBox1.Text = ReadIni(FileLocation, INIkey, INIvalue, "")
ElseIf DialogResult = Windows.Forms.DialogResult.Cancel Then
End If
End Sub
The open file dialog opens and I can open a INI file but the value of INIkey is not placed in TextBox1.
Any idea how I can fix this?
You initialize the path beore the user has selected it:
Dim FileLocation = OpenDLG.FileName.ToString()
Do that after ShowDialog, so here is the correct place:
If DialogResult = Windows.Forms.DialogResult.OK Then
Dim FileLocation = OpenDLG.FileName.ToString()
TextBox1.Text = ReadIni(FileLocation, INIkey, INIvalue, "")
ElseIf DialogResult = Windows.Forms.DialogResult.Cancel Then
End If
But why don't you notice an exception? I assume that you have an empty catch block where you try to open the file in ReadIni. That's a bad habit to kick anyway.
Note that the FileLocation variable in your code references the String OpenDLG.FileName at the time you assign it, there is no permanent connection between both. So if it changes later the variable references still the old string.
The best way to do this would be to make an iniParse module like below and just use the function as shown:
Module iniParse
Public readwrtie As Integer
Public settingValueReturn As New System.Text.StringBuilder(255)
Private Declare Auto Function WritePrivateProfileString Lib "Kernel32" _
(ByVal IpApplication As String, ByVal Ipkeyname As String, _
ByVal IpString As String, ByVal IpFileName As String) As Integer
Private Declare Auto Function GetPrivateProfileString Lib "Kernel32" _
(ByVal IpApplicationName As String, ByVal IpKeyName As String, _
ByVal IpDefault As String, ByVal IPReturnedString As System.Text.StringBuilder, _
ByVal nsize As Integer, ByVal IpFileName As String) As Integer
Public Sub WriteINIFile(heading As String, setting As String, settingvalue As String, path As String)
WritePrivateProfileString(heading, setting, settingvalue, path)
End Sub
Public Sub ReadIniFile(heading As String, setting As String, path As String)
GetPrivateProfileString(heading, setting, "", settingValueReturn, 100, path)
End Sub
End Module
Example:
Button1_click blah blah blah handles button1.click...
ReadIniFile("MAIN", "test", "C:\config.ini")
'this would read the following ini file:
'[MAIN]
'test=hi
'to get that 'hi' value you would use this code:
textbox1.text = settingReturnValue.tostring '(settingValueReturn Will always be the value of the setting entered in the function args)
'to write ini file:
Button1_click blah blah blah handles button1.click...
WriteIniFile("MAIN", "test2", "hi2", "C:\config.ini")
'this would write the following to the ini # C:\config.ini file:
'[MAIN]
'test2=hi2
I hope this helps your needs!

Passing structure as argument through files/functions

I can't get to pass structure as argument through subs/functions in different files of single vbnet project like I use to in earlier MS basic versions.
Here is short example of situation:
Module1.vb
Imports System.IO
Structure mymultitry
Dim index As Integer
<VBFixedString(6)> Dim name As String
Dim weight As Double
End Structure
Module Module1
Public mysetupfile = "mysetup.dat"
Public Sub rwfile(ByVal rw As Integer, ByVal myrecord As Integer, ByVal mmt As mymultitry)
'EDIT: Thanks to SteveDog - proper line should be:
'Public Sub rwfile(ByVal rw As Integer, ByVal myrecord As Integer, ByRef mmt As mymultitry)
Dim fnum As Integer
fnum = FreeFile()
FileOpen(fnum, mysetupfile, OpenMode.Random, OpenAccess.ReadWrite, OpenShare.Shared, Len(mmt))
If rw Then
FilePut(fnum, mmt, myrecord)
Else
FileGet(fnum, mmt, myrecord)
End If
FileClose(fnum)
End Sub
End Module
Form1.vb
Public Class Form1
Dim mmt As mymultitry
Dim mmt1 As mymultitry
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
With mmt
.index = 4
.name = "Helga"
.weight = 128.1445
End With
rwfile(1, 1, mmt) 'write
rwfile(0, 1, mmt1) 'read
'all zero here !?!
Debug.Print(mmt1.index)
Debug.Print(mmt1.name)
Debug.Print(mmt1.weight)
End Sub
End Class
File "mysetup.dat" is reachable and data is saved correctly what I can see with HxD.
But read part seem's to not work as expected.
Please any help on reliable passing structure as argument without too much public elements based on upper example.
I strongly suggest you rewrite your code to use the new .NET IO methods in the System.IO.File class, but, that aside, I think your problem with your existing code is that you need to change your mmt argument from ByVal to ByRef.