|
A ReadOnly folder is easy enough. All you need in the File class and it's GetAttributes[^] method.
You shouldn't be attempting to calculate your own access levels. There's simply so many levels of security and inheritance affecting file access from your code that the best way of reliably testing access is to try to create your file! You can calculate NTFS permissions all you want, but code access permissions can also affect your ability to write a file. What this means is that NTFS can say that it's perfectly OK for you to create a file, but code access permissions can deny you!
Dave Kreskowiak
Microsoft MVP - Visual Basic
|
|
|
|
|
Hi.
can someone pls direct me in how i can go about working out how i can capture printpreview/printdocument data and save into a file for example
doc, txt ,pdf or anything you can assist with..
many thanks.
Robbo
|
|
|
|
|
Are you trying to capture the data that's going to the printer?? Or are you trying to capture an image of the Print Preview?? In your application or any application??
Dave Kreskowiak
Microsoft MVP - Visual Basic
|
|
|
|
|
Hi Dave. Thanks for replying. ive already got a print method working where i can see data in print preview but need to work out if i can capture everything in my print preview and save it to a file whether it'd be a pdf or doc or anything you can advise me with. and YES its for my APP and in my app that i need to do this.
Thanks Dave
Robbo
|
|
|
|
|
You'd use the same code you use to draw your print document to draw to a bitmap if you wanted.
A PDF is a bit of a different story. You'd have to use some kind of PDF library to create the file and you can probably get away with using, again, the same drawing code you're using to generate the print preview.
I don't have any example or knoweldge of any PDF libraries because I simply don't need them and don't use them for anything I do. There's a bunch of them though, just Google for ".net pdf library"[^].
Dave Kreskowiak
Microsoft MVP - Visual Basic
|
|
|
|
|
thanks dave. so what function or commands am i looking to use and to capture the bitmap.
|
|
|
|
|
There really isn't any. You create a bitmap object the size you need, then create a Graphics object from it, pass that to your drawing code and your code should draw everything using that Graphics object. Done. Everything just got drawn to your bitmap. You can do whatever you want with the image then.
Dave Kreskowiak
Microsoft MVP - Visual Basic
|
|
|
|
|
I am having difficulty iterating through an array. I have found the following sample code and tried modifying it with my variables.
' Sample Array Read Code
'For i = 0 To arrList2.Count - 1
' Console.WriteLine(CStr(arrList2.Item(i)))
'Next
After modifying line 3 with x = CStr(arrList2.Item(i)) , I receive the following error: "Value of type 'String' cannot be converted to '1-dimensional array of String'." X is defined as: Dim x() As String
I need to get each element (which is a name read from a textbox) into a variable strName. This is to allow my SQL statement to create a record for each name in the array.
Any help would be greatly appreciated!
Thank you in advance.
LWhite
|
|
|
|
|
Larry White wrote: After modifying line 3 with x = CStr(arrList2.Item(i)) , I receive the following error: "Value of type 'String' cannot be converted to '1-dimensional array of String'." X is defined as: Dim x() As String
It's always best to copy and paste the actual code you're using. Don't post a sample and then tell use what you did to it. It makes it very difficult to see what you actually did.
x doesn't have an Item property, so the code should look more like this:
For i = 0 To x.GetUpperBound(0)
Console.WriteLine(x(i))
Next i
Notice, you don't need CSTR. This is because your x array is an array of Strings. Why try to convert a String to a String??
Also, to get your code to show up in the nice beige box and retain some formatting, paste it between <pre> and </pre> tags.
Dave Kreskowiak
Microsoft MVP - Visual Basic
|
|
|
|
|
Thank you. The tags do make it alot easier to read.
LWhite
|
|
|
|
|
Why do you declare the x variable as an array? As you are going to use it to reference a string, declare it as a string:
Dim x As String
---
single minded; short sighted; long gone;
|
|
|
|
|
I adjusted my code as follows:
Dim x As String
For i = 0 To arrList2.Count - 1
x = arrList2.Item(i)
strName = arrList2.Item(i)
I now get the qty of data entries in my database equal to the number of names I select in the textbox, but the last entry is blank. If I select 3 names, I get the first 2 database entries with the names, but the last one is blank (name field). It appears the first iteration leaves strName blank.
Thanks again for the assistance.
LWhite
|
|
|
|
|
The code that you have shown is correct, although reading the same value from the array twice into separate variables doesn't look like what you want to do.
The problem that you talk about is in the part of the code that you haven't shown. Unless you show it, I can't tell you much more than that it's wrong...
---
single minded; short sighted; long gone;
|
|
|
|
|
Here are the other relevant parts of my code. I build the textbox entries with the following, which also drops the selected names into the array:
Private Sub lbName_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbName.SelectedIndexChanged
editName.Text = Nothing
For i = 0 To lbName.SelectedItems.Count - 1
arrList2.Add(strName)
With arrList2
.ToArray()
End With
editName.Text = editName.Text & lbName.SelectedItems(i) & vbCrLf
strName = lbName.SelectedItems(i) & vbCrLf
Next
End Sub
Here is my complete code that is supposed to create a new database record with each iteration of the name array:
For i = 0 To arrList2.Count - 1
strName = arrList2.Item(i)
x = arrList2.Item(i)
Dim strCommandText As String = "INSERT INTO Orders(OrderDate, DeliverDate, DeliverLocation, Name, Special, Comments, Rate, EMPLID, LoggedIP)" _
& "VALUES('" & strOrderDate & "','" & strDeliverDate & "','" & strDeliverLocation & "','" & strName & "','" & strSpecial & "','" & strComments & "','" & strRate & "','" & strEMPLID & "','" & strLoggedHost & "')"
Dim cmd As New SqlClient.SqlCommand(strCommandText, SqlConnection1)
cmd.ExecuteNonQuery()
Next
Thank you for your assistance.
LWhite
|
|
|
|
|
The code look pretty strange...
Where do you declare the strName variable? You use the strName variable, but you haven't given it any value before using it. You give it a value later in the loop, so it will have a value for the next iteration of the loop, but not for the first iteration. Also, the value that you assign to it in the last iteration will never be used.
You create an array from the items in the list arrList2, but you just throw the array away without doing anything with it. Why?
---
single minded; short sighted; long gone;
|
|
|
|
|
When the form opens, I have a "Dim strName As String" along with other variables. Thank you, you got me thinking and I rearranged my code. It works now. Here is what it looks like now:
Private Sub lbName_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbName.SelectedIndexChanged
editName.Text = Nothing
tbEmplid.Text = Nothing
For i = 0 To lbName.SelectedItems.Count - 1
strName = lbName.SelectedItems(i) & vbCrLf '.ToString & vbCrLf
arrList2.Add(strName)
With arrList2
.ToArray()
End With
editName.Text = editName.Text & lbName.SelectedItems(i) & vbCrLf
Next
End Sub
Note: I moved the "strName = lbName.SelectedItems(i) & vbCrLf" line above the "arrList2.Add(strName)" line, instead of being the last line. It sequences through nicely now. I trash the array, is it is only for the record creation and not needed after that.
Thank you again! I hope posting this code will help someone else!
LWhite
|
|
|
|
|
That looks better.
Any variables you can, you should declare locally inside the method, instead of declaring them in the class. Local variables are allocated on the stack, are very inexpensive, and only exist when they are needed. Class variables increases the size of your form object, and they take up memory even when they are not used.
You still have some strange code...
With arrList2
.ToArray()
End With
This does the same as:
Dim s() as String
With attList2
s = .ToArray()
End With
s = Nothing
In other words, the only thing that it does is wasting time and memory resources. You create one array each iteration of the loop, and not any of the arrays are ever used for anything.
---
single minded; short sighted; long gone;
|
|
|
|
|
 Thank you for your suggestions. I rearranged most of my variables to be local. I have another issue you may be able to help me on. I'm using the following code to perform a SQL SELECT to combine the first & last name, and the employee ID number to be used in a listbox. The user selects multiple names that are transfered to a textbox. When they are selected, the array is created, which is used to create a record in a SQL table (one entry per name). The problem I have is, I need to update the Employee ID field but haven't found the right code to extract the "emplid" data. The name field works fine.
Dim sql1 As String = "Select (LastName + ', ' + FirstName) As Name, emplid FROM Users ORDER BY LastName"
Dim strCommandText As String = "INSERT INTO Orders(OrderDate, DeliverDate, DeliverLocation, Name, Special, Comments, Rate, EMPLID, LoggedIP)" & "VALUES('" & strOrderDate & "','" & strDeliverDate & "','" & strDeliverLocation & "','" & strName & "','" & strSpecial & "'" & ",'" & strComments & "','" & strRate & "','" & strEMPLID & "','" & strLoggedHost & "')"
Private Sub lbName_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbName.SelectedIndexChanged
Dim i As Integer
editName.Text = Nothing
For i = 0 To lbName.SelectedItems.Count - 1
arrList2.Add(lbName.SelectedItems(i))
With arrList2
.ToArray()
End With
editName.Text = editName.Text & lbName.SelectedItems(i) & vbCrLf
Next
i = Nothing
End Sub
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
Dim i As Integer
Dim r As String
Dim x As String
editComments.Clear()
strComments = editComments.Text
Try
SqlConnection1.Close()
cnn1.Close()
SqlConnection1.Open()
' Create a name array from editName textbox; Used to create a new record for each name
For i = 0 To arrList2.Count - 1
strName = arrList2.Item(i)
x = arrList2.Item(i)
Dim strCommandText As String = "INSERT INTO Orders(OrderDate, DeliverDate, DeliverLocation, Name, Special, Comments, Rate, EMPLID, LoggedIP)" & "VALUES('" & strOrderDate & "','" & strDeliverDate & "','" & strDeliverLocation & "','" & strName & "','" & strSpecial & "'" & ",'" & strComments & "','" & strRate & "','" & strEMPLID & "','" & strLoggedHost & "')"
Dim cmd As New SqlClient.SqlCommand(strCommandText, SqlConnection1)
cmd.ExecuteNonQuery()
Next
Catch eEndEdit As System.Exception
System.Windows.Forms.MessageBox.Show(eEndEdit.Message)
Finally
If strRate = strRateBreakfast Then
MessageBox.Show("Galley orders for BREAKFAST on: " & Now() & " have been successfully entered!", MessageBoxIcon.Information)
ElseIf strRate = strRateDinner Then
MessageBox.Show("Galley orders for DINNER on: " & Now() & " have been successfully entered!", MessageBoxIcon.Information)
End If
SqlConnection1.Close()
cmd = Nothing
End Try
Close()
End Sub
LWhite
|
|
|
|
|
Hi Guys,
I'm facing a problem in serializing a class object in VB.Net. In this case, the
class contains two public properties and an arraylist . Class structure and Code for
serialization are given below. It serializes only the public properties, not the
arraylist. It throws a "System.InvalidOperationException..The type Photos was not
expected. Use the XmlInclude or SoapInclude attribute to specify types that are not
known statically". I have no idea about where to include these attributes... Pls
help..
<br />
<br />
'Class Definition<br />
<br />
<Serializable()> _<br />
Public Class SitePhotos<br />
Private _Outdoordesc As String<br />
Public Property Outdoordesc() As String<br />
Get<br />
Return _Outdoordesc<br />
End Get<br />
Set(ByVal value As String)<br />
_Outdoordesc = value<br />
End Set<br />
End Property<br />
<br />
Private _ShelterEntrance As String<br />
Public Property ShelterEntrance() As String<br />
Get<br />
Return _ShelterEntrance<br />
End Get<br />
Set(ByVal value As String)<br />
_ShelterEntrance = value<br />
End Set<br />
End Property<br />
<br />
'Arraylist to store photo descriptions<br />
<br />
Public _PhotoList As New System.Collections.ArrayList<br />
<br />
Public Sub AddPhoto(ByVal photoObj As Photos)<br />
_PhotoList.Add(photoObj)<br />
End Sub<br />
<br />
Public Sub RemovePhoto(ByVal range As String)<br />
Dim _pht As Photos<br />
'Dim i As Integer<br />
<br />
<br />
For Each _pht In _PhotoList<br />
<br />
If _pht.posRange = range Then<br />
_PhotoList.RemoveAt(_PhotoList.IndexOf(_pht))<br />
End If<br />
<br />
Next<br />
<br />
End Sub<br />
<br />
<br />
Public ReadOnly Property PhotoList() As ArrayList<br />
Get<br />
Return _PhotoList<br />
End Get<br />
End Property<br />
<br />
End Class<br />
<br />
'Structure of each photo description<br />
Public Structure Photos<br />
Public Pos As Integer<br />
Public Desc As String<br />
Public posRange As String<br />
End Structure<br />
<br />
<br />
<br />
'Code for serialization<br />
<br />
Dim extraTypes(0) As Type<br />
<br />
extraTypes(0) = GetType(SitePhotos)<br />
<br />
Dim xSerializer As XmlSerializer = New<br />
XmlSerializer(GetType(GeneralSiteInfo.SitePhotos), extraTypes)<br />
<br />
Dim xWriter As StringWriter = New StringWriter()<br />
<br />
' Since the serialization can exception, we'll encase it in a try/catch<br />
' mechanism, so we can report the error properly<br />
<br />
Try<br />
xSerializer.Serialize(xWriter, objPhotos)<br />
<br />
'xmlSitePhotos is a hidden control to which i can store value. <br />
<br />
_xmlSitePhotos.Value = xWriter.ToString()<br />
<br />
Catch ex As Exception<br />
<br />
MsgBox(ex.ToString())<br />
'MsgBox("Could not serialize Site Photos", MsgBoxStyle.Critical, "Error<br />
Saving Site Photos")<br />
<br />
End Try<br />
<br />
xWriter.Close()<br />
<br />
<br />
thanks,
Sebastian
-- modified at 13:10 Friday 9th February, 2007
|
|
|
|
|
Add this line just before your SitePhotos Class
<Serializable(), Xml.Serialization.XmlInclude(GetType(Photos))> _
|
|
|
|
|
 Thank you so much Friend!!!
It is working well with the changes you have suggested.
I also have made the following changes:
<br />
<br />
'Class Definition<br />
<br />
<Serializable() , Xml.Serialization.XmlInclude(GetType(Photos))> _<br />
Public Class SitePhotos<br />
Private _Outdoordesc As String<br />
Public Property Outdoordesc() As String<br />
Get<br />
Return _Outdoordesc<br />
End Get<br />
Set(ByVal value As String)<br />
_Outdoordesc = value<br />
End Set<br />
End Property<br />
<br />
Private _ShelterEntrance As String<br />
Public Property ShelterEntrance() As String<br />
Get<br />
Return _ShelterEntrance<br />
End Get<br />
Set(ByVal value As String)<br />
_ShelterEntrance = value<br />
End Set<br />
End Property<br />
<br />
'Arraylist to store photo descriptions<br />
<br />
Public PhotoList As New System.Collections.ArrayList<br />
<br />
Public Sub AddPhoto(ByVal photoObj As Photos)<br />
PhotoList.Add(photoObj)<br />
End Sub<br />
<br />
Public Sub RemovePhoto(ByVal range As String)<br />
Dim _pht As Photos<br />
'Dim i As Integer<br />
<br />
<br />
For Each _pht In _PhotoList<br />
<br />
If _pht.posRange = range Then<br />
PhotoList.RemoveAt(PhotoList.IndexOf(_pht))<br />
End If<br />
<br />
Next<br />
<br />
End Sub<br />
<br />
<br />
End Class<br />
<br />
'Structure of each photo description<br />
Public Structure Photos<br />
Public Pos As Integer<br />
Public Desc As String<br />
Public posRange As String<br />
End Structure<br />
<br />
<br />
<br />
'Code for serialization<br />
<br />
Dim extraTypes(0) As Type<br />
<br />
extraTypes(0) = GetType(SitePhotos)<br />
<br />
Dim xSerializer As XmlSerializer = New<br />
XmlSerializer(GetType(GeneralSiteInfo.SitePhotos), extraTypes)<br />
<br />
Dim xWriter As StringWriter = New StringWriter()<br />
<br />
' Since the serialization can exception, we'll encase it in a try/catch<br />
' mechanism, so we can report the error properly<br />
<br />
Try<br />
xSerializer.Serialize(xWriter, objPhotos)<br />
<br />
'xmlSitePhotos is a hidden control to which i can store value. <br />
<br />
_xmlSitePhotos.Value = xWriter.ToString()<br />
<br />
Catch ex As Exception<br />
<br />
MsgBox(ex.ToString())<br />
'MsgBox("Could not serialize Site Photos", MsgBoxStyle.Critical, "Error<br />
Saving Site Photos")<br />
<br />
End Try<br />
<br />
xWriter.Close()<br />
<br />
<br />
|
|
|
|
|
the button I had in mind is something like a piano key that produces sound when pressed...
First i would like to know d codings on how to play a sound file (winamp) by clicking a button in a vb2003 form........
I might want to have a few of those, each playing a different note, (like button A would play file A)---i have a few files for each note already.I just don't know d coding to play those file in the event button A is pressed.And what if my sound files are all .mdi (MIDI)format??
The nex thing is, once i've pressed buttons A, B and C in sequence, i want it to be saved into the database---and i would like to play d saved sequence from database (sql server) later.So, can any kind soul out there please show me d codings??
Thanx!!
|
|
|
|
|
Bonjour,
1°) j'aimerais savoir comment reconnaître d'avance un répertoire en lecture
seul sans devoir attendre le code erreur de retour en tentative d'écriture
sur celui-ci comme on peut le faire pour savoir si un fichier est système,
archive, lecture, caché.
2°) j'aimerais savoir aussi comment détecter les autorisations (permissions,
comptes autorisés) d'écriture sur un répertoire sans devoir attendre le code
erreur de retour en tentative d'écriture sur celui-ci comme on le fait pour
un fichier.
Cette question est indépendante de la première car le répertoire peut être
en lecture/écriture mais que pour certaines personnes donc inutile d'aller
plus loin s'il est déjà en lecture seul.
j'ai essayé par le code suivant différents tests:
Dim FolderBrowserDialog1 As FolderBrowserDialog = New FolderBrowserDialog()
Dim MyAttr As FileAttribute
If FolderBrowserDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
MyAttr = GetAttr(FolderBrowserDialog1.SelectedPath)
MsgBox(MyAttr.ToString) 'me retourne Directory
MsgBox(File.GetAttributes(FolderBrowserDialog1.SelectedPath).ToString)
'me retourne Directory
Dim information As System.IO.FileInfo
information =
My.Computer.FileSystem.GetFileInfo(FolderBrowserDialog1.SelectedPath)
MsgBox(information.IsReadOnly) 'me retourne toujours false même si le
répertoire est en lecture seul
End If
d'avance merci à tous
|
|
|
|
|
CP is an English-speaking board, and the French/English babelfish translator isn't doing a good job.
Dave Kreskowiak
Microsoft MVP - Visual Basic
|
|
|
|
|
Bonjour - pardonnez/excusez moi, je parle un petite peu de Francais (je suis tres desolate) ...
pour >> 1°) j'aimerais savoir comment reconnaître d'avance un répertoire en lecture seul sans devoir attendre le code erreur de retour en tentative d'écriture sur celui-ci comme on peut le faire pour savoir si un fichier est système, archive, lecture, caché.
regardez http://www.codeproject.com/file/fileinfo.asp[^]
et pour 2°) j'aimerais savoir aussi comment détecter les autorisations (permissions,
comptes autorisés) d'écriture sur un répertoire sans devoir attendre le code
erreur de retour en tentative d'écriture sur celui-ci comme on le fait pour
un fichier.
regardez (peut-être) http://www.devx.com/cplus/Article/16711[^]
essayez svp de parler anglais, ici
'g'
|
|
|
|
|