473,511 Members | 17,164 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Efficient way to write to a text file and append?

1 New Member
I am familiar with two ways of writing to a text file so the data persists when you access it again. Adding more data (from textbox and label) will be necessary for future transactions. I'd like to learn how to write the code so I don't get an UnauthorizedAccessException class.

I am using this way, resulting in error:

Expand|Select|Wrap|Line Numbers
  1. Dim objwriter As New System.IO.StreamWriter("C:\Customer Transactions.txt")
  2.  
  3.         objwriter.Write(txtCustName.Text, lblOrderNo, lblFinalCost)
  4.         objwriter.Close()

And there is this way (with errors again):
Expand|Select|Wrap|Line Numbers
  1.   Dim myStream As Stream
  2.         Dim saveFileDialg As New SaveFileDialog()
  3.  
  4.         saveFileDialg.Filter = ("C:\Customer Transactions.txt")
  5.         saveFileDialg.FilterIndex = 2
  6.         saveFileDialg.RestoreDirectory = True
  7.  
  8.         If saveFileDialg.ShowDialog() = DialogResult.OK Then
  9.             myStream = saveFileDialg.OpenFile()
  10.             If (myStream IsNot Nothing) Then
  11.                 myStream.WriteLine(txtFullName.Text, lblOrder, lblTotal)
  12.                 myStream.Close()
  13.                 myStream.Close()
  14.             End If
  15.         End If

I'm sure there is something I am not quite getting. Some guidance would be appreciated.
Dec 16 '13 #1
1 1305
Frinavale
9,735 Recognized Expert Moderator Expert
The Windows user that is running your application does not have permissions to create files on C:\. You need administrative privileges to change any files in the C:\ root directory.

Try writing the file into the application's executing path instead. For information about getting the application's path check out this article
[url=http://msdn.microsoft.com/en-us/library/aa457089.aspx]
HOW TO: Determine the Executing Application's Path[/icode].

You could also consider writing into the ProgramData path (for example you could try using C:\ProgramData\YourApplicationName).

-Frinny
Dec 17 '13 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
5765
by: Google Mike | last post by:
I have PHP version 4.2.2 that ships with RH9. I want to have it write to a file like this: function WriteLog($sLogFile, $sMsg) { if (substr($sLogFile,1,1) != '/') { $sLogFile =...
1
15007
by: Gurk | last post by:
hallow all i'm using a common dialog to save data into a text file. for so far i have this, but can someone give me a simpe exaple what i have to do after the line Open cdl.FileName For Output...
1
10086
by: WebSim via .NET 247 | last post by:
Hi, I want to read a text file, and then write it into dataset. Is it possible? or Is there any solution that read text file into DB? ----------------------- Posted by a user from .NET 247...
4
3423
by: AHP | last post by:
Hi, I'm using Visual Studio 2005. I am developing a web application that uses the FileUpload control to upload text files to a directory on a webserver. This works fine. However, for me to be...
13
4163
by: DH | last post by:
Hi, I'm trying to strip the html and other useless junk from a html page.. Id like to create something like an automated text editor, where it takes the keywords from a txt file and removes them...
3
9556
by: mainul | last post by:
Hi guys i can write text file and also can read it. below are the codes. <?PHP //write to a file $body_content="This is my content"; // Store some text to enter inside the file...
9
6917
by: andy.z | last post by:
If 2 people try to access the same text file at the same time to write to it - what happens in PHP ? What I mean is - presumably the first will be ok - But what will the second person actually...
1
1976
by: lokeshrajoria | last post by:
hello, how can i write text file text data to binary data in binary file. Thanks & Regards Lokiii
2
3049
by: Andez | last post by:
I've wrote a windows service that performs simple functions within our application. To ensure safe running of the service - if it errors we want to know where when how - it logs to a text file - in...
3
3202
by: Pete Martinez | last post by:
Greetings, I need a simple method using ActiveXobject in javascript to write a text file from the local drive to my webserver. I found a method on a previous thread that copies a file from the...
0
7242
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7138
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7423
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7081
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5668
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5066
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3213
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1576
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
447
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.