[RESOLVED] Wanting to write to file without admin rights
I am trying to write to a text file in a certain location
Code:
Dim W As New System.IO.StreamWriter("C:\saves.txt")
W.Write(whatever data I want written
more data)
W.Close()
but every time it throws me an error. ('Access to the path 'C:\saves.txt' is denied.')
I don't want to use admin settings for this. Also I don't want to save the text file in the same place as the executable.
It works if it saves in the same place as the executable and all the data is written. If I try saving it under the ("users/public") folder, only a little bit of the data that I want written is there, not all the data.
Re: Wanting to write to file without admin rights
If you want to write to a file without admin privileges then you have to write to a location that doesn't require admin privileges. You can and should use either Environment.GetFolderPath or My.Computer.FileSystem.SpecialDirectories to get the paths of various special folders defined by Windows. Some of those are appropriate and some aren't. Some require admin privileges and some don't. Do some research and, if required, some testing and see which is the best option in your case.
Re: Wanting to write to file without admin rights
I am definitely doing something wrong, I tried
Code:
Dim W As New System.IO.StreamWriter ("saves.txt", Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments))
And I’m getting
System.InvalidCastException: 'Conversion from string "C:\Users\\Documents" to type 'Boolean' is not valid.'
Re: Wanting to write to file without admin rights
Yeah, that code makes no sense. Read the documentation for the StreamWriter class or just pay attention to what Intellisense tells you to see what constructors the class actually has. There certainly isn't one with two String parameters for the file name and folder path. You need to create the full file path first, then pass that as a single String.
You should also turn Option Strict On in the project properties, which would have told you that you were doing the wrong thing at compile time, instead of letting it get through to run time. You should turn it On in the VS options as well, so it is On by default for all future projects.
Re: Wanting to write to file without admin rights
Great! I got that part figured out. Now I’m having a problem where half the data won’t be read. All the data is written but unless I have the save file in the same place as the executable, it will only read half of the saved data. Is there a way I can fix this?
Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
Dim path As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\saves.txt"
Dim read As New System.IO.StreamReader(path, True)
Dim saves As String = read.ReadToEnd
calc = saves.Split("!")(0)
cps = saves.Split("!")(1)
pquantity = saves.Split("!")(2)
pprice = saves.Split("!")(3)
fquantity = saves.Split("!")(4)
fprice = saves.Split("!")(5)
miquantity = saves.Split("!")(6)
miprice = saves.Split("!")(7)
micquantity = saves.Split("!")(8)
micprice = saves.Split("!")(9)
piquantity = saves.Split("!")(10)
piprice = saves.Split("1")(11)
vquantity = saves.Split("!")(12)
vprice = saves.Split("!")(13)
rquantity = saves.Split("!")(14)
rprice = saves.Split("!")(15)
lquantity = saves.Split("!")(16)
lprice = saves.Split("!")(17)
mquantity = saves.Split("!")(18)
mprice = saves.Split("!")(19)
bquantity = saves.Split("!")(20)
bprice = saves.Split("!")(21)
read.Close()
Catch ex As Exception
End Try
NumberFormat()
End Sub
Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
Dim path As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\saves.txt"
Dim W As New System.IO.StreamWriter(path, True)
W.Write(score & "!" & cps & _
"!" & pquantity & "!" & pprice _
& "!" & fquantity & "!" & fprice _
& "!" & miquantity & "!" & miprice _
& "!" & micquantity & "!" & micprice _
& "!" & piquantity & "!" & piprice _
& "!" & vquantity & "!" & vprice _
& "!" & rquantity & "!" & rprice _
& "!" & lquantity & "!" & lprice _
& "!" & mquantity & "!" & mprice _
& "!" & bquantity & "!" & bprice)
W.Close()
End Sub
Re: Wanting to write to file without admin rights
First things first, when you save the data to the file, you are telling it to append to the existing text rather than overwrite it. Is that really what you want? I suspect not, but I could be wrong.
Re: Wanting to write to file without admin rights
How would I go about overwriting instead of appending? The data writes fine, Its just whenever the save file is saved in a folder besides the location of the executable it writes all of the data but it will only read like half of the data even though everything is written. I just want all the data to be read and that isn't happening!
EDIT: It's not writing all the data. My first impression was that It couldn't read. It wont read it and it wont write all of it.
Re: Wanting to write to file without admin rights
Quote:
Originally Posted by
Yourmomsfire
How would I go about overwriting instead of appending?
As I already told you:
Quote:
Read the documentation for the StreamWriter class or just pay attention to what Intellisense tells you to see what constructors the class actually has.
Both Intellisense and the documentation will tell you what constructors are available and what each parameter of each one means. The information is freely available to you but you just ignored it.
Quote:
Originally Posted by
Yourmomsfire
It's not writing all the data. My first impression was that It couldn't read. It wont read it and it wont write all of it.
It's a concern that you are only reporting this now. The very first thing you should have been doing was opening the file to see what it contained. Either it contains all the data or it doesn't. If it doesn't then the reading part is irrelevant, for now at least.
Re: Wanting to write to file without admin rights
If you want to write a single String to a new file then do it the easy way:
vb.net Code:
File.WriteAllText(filePath, text)
If the text you want to write is a bunch of substrings, separated by a delimiter, create the complet String using the String.Join method:
vb.net Code:
Dim text = String.Join("!", {score, cps, pquantity, ..., bprice})
That said, it seems bit silly to create a single line of text using "!" as a delimiter when you could just write the data on multiple lines and make the file more readable as a result:
vb.net Code:
File.WriteAllLines(filePath, {score, cps, pquantity, ..., bprice})
That will write the specified array to the file with each element on its own line. You can then read the file contents back into an array by calling File.ReadAllLines.
Re: Wanting to write to file without admin rights
This part of your code is pretty terrible:
Quote:
Originally Posted by
Yourmomsfire
Code:
Dim saves As String = read.ReadToEnd
calc = saves.Split("!")(0)
cps = saves.Split("!")(1)
pquantity = saves.Split("!")(2)
pprice = saves.Split("!")(3)
fquantity = saves.Split("!")(4)
fprice = saves.Split("!")(5)
miquantity = saves.Split("!")(6)
miprice = saves.Split("!")(7)
micquantity = saves.Split("!")(8)
micprice = saves.Split("!")(9)
piquantity = saves.Split("!")(10)
piprice = saves.Split("1")(11)
vquantity = saves.Split("!")(12)
vprice = saves.Split("!")(13)
rquantity = saves.Split("!")(14)
rprice = saves.Split("!")(15)
lquantity = saves.Split("!")(16)
lprice = saves.Split("!")(17)
mquantity = saves.Split("!")(18)
mprice = saves.Split("!")(19)
bquantity = saves.Split("!")(20)
bprice = saves.Split("!")(21)
Why call Split over and over and just take one element each time? Why not just split once, assign the result to a variable and then use that one array over and over:
Code:
Dim saves As String = read.ReadToEnd
Dim parts = saves.Split("!")
calc = parts(0)
cps = parts(1)
pquantity = parts(2)
pprice =parts(3)
'...
bquantity = parts(20)
bprice = parts(21)
Never use the same complex expression multiple times. Always either use a With block or else use the expression once, assign the result to a variable and use the variable multiple times. If you use line breaks as your delimiter and call ReadAllLines then that will return an array, so this is a moot point.
Re: Wanting to write to file without admin rights
It seems that you have started a new thread for what is actually a new problem. That is a good thing but now I've responded to you here and so you have the same issue on the go in two places, which is bad. If the issue you brought up in this thread is resolved then you should mark this thread Resolved, using the Thread Tools menu, and the new issue can be addressed in the new thread. It also appears that the new thread is wrong, given that you've said here that the problem is writing the file. It would help if you could provide a clear explanation of the problem. It would also help if you debugged and tested your code properly and, if there's still an issue, provided us with steps to reproduce the issue.
Re: Wanting to write to file without admin rights
Quote:
Originally Posted by
jmcilhinney
As I already told you:
Both Intellisense and the documentation will tell you what constructors are available and what each parameter of each one means. The information is freely available to you but you just ignored it.
It's a concern that you are only reporting this now. The very first thing you should have been doing was opening the file to see what it contained. Either it contains all the data or it doesn't. If it doesn't then the reading part is irrelevant, for now at least.
It writes the base data which is a bunch of numbers. If i change something in the program itself. nothing changes in the file.
Re: Wanting to write to file without admin rights
Quote:
Originally Posted by
Yourmomsfire
If i change something in the program itself. nothing changes in the file.
Is that really the case? Did you actually look at the file to determine that or did you just rely on what you see in your application when you run it? I reckon it was the latter. Think about it. I told you that you are appending to the file instead of overwriting it. When you read the file, you are only using the text at the beginning. If you just keep appending new text to the file then of course that won't affect the text at the beginning, so you will just keep using the same text over and over.
This is an example of why you need to think like a developer, not like a user. When you write to the file, open the file and look at what it contains. If you had done that then it would be obvious that you're appending rather than overwriting. When you read the data, look at the data you actually read, i.e. the saves variable. If you had done that then it would be obvious you are using parts of that text from the beginning that don't change while the new data is appended to the end and ignored. The issue here is almost certainly the fact that you are appending data rather than overwriting it. You shouldn't have been doing that in the first place but I pointed that out a couple of hours ago and told you what you need to do to address it. Why haven't you addressed that yet?
Re: Wanting to write to file without admin rights
So I tried simplifying the code with
Code:
Dim text = String.Join("!", {score, cps, pquantity, pprice})
But it throws me an error
Code:
Overload resolution failed because no accessible 'Join' is most specific for these arguments: 'Public Shared Overloads Function Join(separator As String, ParamArray values As Object()) As String': Not most specific. 'Public Shared Overloads Function Join(Of Double)(separator As String, values As IEnumerable(Of Double)) As String': Not most specific.
Re: Wanting to write to file without admin rights
Quote:
Originally Posted by
Yourmomsfire
So I tried simplifying the code with
Code:
Dim text = String.Join("!", {score, cps, pquantity, pprice})
But it throws me an error
Code:
Overload resolution failed because no accessible 'Join' is most specific for these arguments: 'Public Shared Overloads Function Join(separator As String, ParamArray values As Object()) As String': Not most specific. 'Public Shared Overloads Function Join(Of Double)(separator As String, values As IEnumerable(Of Double)) As String': Not most specific.
I don't know what type the values are that you're putting in that array but you can force it to being an Object array like so:
vb.net Code:
Dim text = String.Join("!", New Object() {score, cps, pquantity, pprice})
That said, if they are not all the same type then I would have expected that the compiler would have flagged that as an issue first. Did you turn Option Strict On? It seems like you probably didn't and now yet another reason that you should has reared its ugly head.
Re: Wanting to write to file without admin rights
Great! it is working perfect now! Thank you for your help and teachings!