I'm using IsloatedStorage in a Silverlight app to log information on the client, and I added a function to clear the log file. However, I have had problems with the two approaches I tried:
Approach one: use
IsolatedStorageFile.DeleteFile("log.log");
Result: This fails and returns an "[IsolatedStorage_DeleteFile]" error (No other info). The function works fine on test files, e.g. DeleteFile("test.txt"), but refuses to delete the log. I though that perhaps the log is being used, and tried to close it with
IsolatedStorageFileStream.close()
But this returns a different error "[IsolatedStorage_StoreNotOpen]". I know it is open as the previous line of code successfully logs a message.
Approach Two: Reopen the log file using the Truncate file mode,
_storageFileStream = new IsolatedStorageFileStream(logfilename, FileMode.Truncate, FileAccess.Write, FileShare.ReadWrite, _storageFile);
According to MSDN, Truncate "Specifies that the operating system should open an existing file. Once opened, the file should be truncated so that its size is zero bytes." However, it opens my log file and fills it with blank space! The filesize is left identical, the next log message is appended to the end of all of the space.
I've found a way to do this, not by closing but by disposing:
IsolatedStreamWriter.Dispose();
IsolatedStorageFile.DeleteFile("log.log");
IsolatedStorageFileStream = new IsolatedStorageFileStream(logfilename, FileMode.Create, FileAccess.Write, FileShare.ReadWrite, _storageFile);
Didn't get the 'truncate' approach to work, but no need now.
Related
I've been stuck on this issue for some time now, i have connected to to an Azure database then created a dataset by adding a new data source.
But whenever i try to preview the data in one of the tables this error messeges
Could not get type information for app.dataset
The error message pops up whenever that dataset is created, and this error message pops up when i run this code
var q = (from ev in db.Events
where ev.Id != 0
select ev.Title).First().ToString();
string e = q;
tv.Text = e;
Sequence contains no elements
Any help is appreciated
I had the same kind of problem with Visual Studio 15 and a regular SQL server. The Connection string was stored in my settings file. I had copied the file from another project and made small adjustments to it.
Deleting the copied settings file and creating a fresh one solved the problem. Apparently some incorrect data was copied also, even tho the settings file looked just fine.
I'm using Visual basic 2012, and I'm experimenting with manipulating .txt files. I've managed to make a button to create them, but the button I made to delete them always runs into and error, claiming the file is still in use. I've tried to write code to close the file, but no success. The closest I've gotten was when I tried
FileStream.Close("C:\Testfile")
But I get an error saying that It needs to be linked to an object. I don't have the faintest idea what it mean by object, and I don't have any other ideas
Can someone tell me what I need to do to fix this, or alternatively, another solution
You must make sure that the code you have for creating the file, closes that file as well after the job is done:
Dim sw As StreamWriter = New StreamWriter("C:\Testfile")
' write to the file here, like:
sw.Write("...")
' ...etc, and at the end of the job, close the file:
sw.Close()
To actually delete a file from the file system, use:
File.Delete("C:\Testfile")
I suspect the problem that you are having could be because the file is still open when you are trying to delete it.
Add your file manipulation code inside a Using block as explained in this MSDN article. By using this technique you will not have to remember to close files yourself as it will happen automatically once you have left the Using block.
Dim filePath As String = "hello.txt"
Using openedFile As FileStream = File.Open(filePath, FileMode.OpenOrCreate)
'File is OPEN
Dim bytes() As Byte = Encoding.ASCII.GetBytes("Hello World")
openedFile.Write(bytes, 0, bytes.Length)
End Using
'File is CLOSED and can be deleted
File.Delete(filePath)
You can also use this technique for any other objects that implement IDisposable such as StreamReader and StreamWriter.
I've placed a custom entry in the win.ini file in the windows directory , called LoginCount under a custom [Login] section and increment it each time the main view is loaded. In the Form Create event I access the win.ini and check for its value and if it's reached 1000 I show a message. But a very weird thing has happened. After reaching 1000 I manually set its value back to 0. But it still shows 1000. I checked the path, in case the application refers to some other win.ini file somewhere in the system, but it's C:\Windows\win.ini. Here's the code:
procedure TfMain.FormCreate(Sender: TObject);
var
winIni: TIniFile;
windir_buf: array [0 .. 144] of Char;
WINDIR: string;
loginCount: integer;
begin
GetWindowsDirectory(windir_buf, sizeof(windir_buf));
WINDIR := StrPas(windir_buf) + '\';
ShowMessage(WINDIR+'win.ini');//Shows C:\Windows\win.ini
winIni := TIniFile.Create(WINDIR + 'Win.ini');
loginCount := winIni.ReadInteger('Login', 'LoginCount', 1);
ShowMessage(IntToStr(loginCount));//Shows 1000 although it's 0 in the actual file.
end;
Any idea why?
This is probably due to file system virtualization. You are running a virtualized process under Vista or later and don't have write access to the Windows directory.
There's no point debugging this. The Win.ini file has been deprecated for nearly 20 years. You should:
Add a manifest to suppress virtualization.
Ensure that you run with UAC enabled, and as standard user.
Store your file to an appropriate folder. Under the user profile is the obvious place. That makes it a per-user setting. For system-wide settings you store to the ProgramData folder.
If you simply cannot bring yourself to stop using Win.ini then you'll still need to add a manifest with the requireAdministrator option.
If you are going to use Win.ini (and I cannot express how much I abhor the fact you contemplate doing so) then you should use GetProfileString and SetProfileString. Since the file is shared you need to access it with functions that synchronize that access.
I have been trying to write and read files directly from the BlobStore, but it just doesnt work.
The issue is I open the file like file = fileService.getBlobFile(blobKey); and it doesn't throw any exception but right in the next line I call readChannel = fileService.openReadChannel(file, false); and that one throws a FileNotFoundException.
I'm confused as to why the first line did not throw the exception.
Here is the same issue
Unfortunately no one answered that question.
I haven't had any problems with writes or deletes, but I too get a FileNotFoundException when using openReadChannel(...) with an AppEngineFile.
I've tried using an AppEngineFile created from its constructor taking a complete path. I've tried using an AppEngineFile obtained from getBlobFile(...) like you do above. Either way, when the AppEngineFile is passed to openReadChannel(...) a FileNotFoundException is thrown.
My workaround was to let BlobstoreService.serve(...) do all the work of reading and sending the file. I suspect that using the FileService to read from an AppEngineFile isn't supported yet (I'm using 1.6.0), so reads must be done via the BlobstoreService (serve(...), fetchData(...), BlobstoreInputStream).
I had a connection string to a MS Access DB file, Foo.accdb, defined and used in my project. It was defined as a connection string Setting in the Settings section of my project properties. The program referenced the connection string setting and everything worked fine.
Then I decided to replace Foo.accdb with two different DB files, A.accdb and B.accdb each of which would be used under different circumstances. I added connection strings for them in Settings and removed the Setting definition for Foo.accdb connection string.
The name of my application is Foo and the name of the Foo.accdb connection string was FooConnectionString.
But now when I build the program both in debugger and for release I get the following error message:
'FooConnectionString' is not a member of 'Foo.My.MySettings'.
The offending reference, in file FooDataSet.Designer.vb, is:
<Global.System.Diagnostics.DebuggerNonUserCodeAttribute()> _
Private Sub InitConnection()
Me._connection = New Global.System.Data.OleDb.OleDbConnection
Me._connection.ConnectionString = Global.Foo.My.MySettings.Default.FooConnectionString
End Sub
What is going on here? FooConnectionString is not in any other file in the project directory nor in the My Project subdir. I completely got rid of it in my code and in my project properties yet it persists in FooDataSet.Designer.vb (whatever that is).
While researching this on the web I saw a recommendation to select the FooDataSet.xsd file, right click it and execute the "Run Custom Tool" option. I did this and it appears to rebuild FooDataSet.Designer.vb (the time stamp changes) but the problem persists.
I also tried removing the offending reference by manually editing FooDataSet.Designer.vb but that gave me some other error message.
Why is this old reference staying around and what can I do about it?
This is a standalone app. I'm using VS2008 Standard Ed., VB.Net 3.5
Thanks.
Open the FooDataSet XSD file in a text editor. Right click on dataset in the solution explorer and select "Open With..." and the select XML (text) Editor or open it outside the solution.
Look for the <Connections> tag near the top of the file. Remove the line that looks like this
<Connection AppSettingsObjectName="Settings" AppSettingsPropertyName="FooConnectionString" ConnectionStringObject="" IsAppSettingsProperty="true" Modifier="Assembly" Name="FooConnectionString(Settings)" ...