I want to store certain .txt file during app lifetime. I was thinking to use Environment.SpecialFolder.ApplicationData
so I created
string myFilename = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), address.GetHashCode() + ".txt");
...
File.WriteAllText(myFilename, someTextContent);
I'm getting
Cannot create file "c:\User...\Appdata\Roaming...". Access is denied.
Consider using Isolated Storage, you have guaranteed read/write access with it.
Writing:
IsolatedStorageFile isoStore = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null);
using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream("TestStore.txt", FileMode.CreateNew, isoStore))
{
using (StreamWriter writer = new StreamWriter(isoStream))
{
writer.WriteLine("Hello Isolated Storage");
}
}
Reading:
using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream("TestStore.txt", FileMode.Open, isoStore))
{
using (StreamReader reader = new StreamReader(isoStream))
{
string contents = reader.ReadToEnd();
}
}
Related
When I deploy my changes to azure web app I am getting below exception :
enter image description here
But no local it's working fine.
I have check my file IsReadOnly=false or not.
Using a below code to achieve
var fileName = "myfilename.json";
var filePath = Path.Combine(Directory.GetCurrentDirectory(), fileName);
var configJSON = JsonConvert.SerializeObject(integrationsConfiguration);
cancellationToken.ThrowIfCancellationRequested();
FileInfo fileInfo = new FileInfo(fileName);
fileInfo.IsReadOnly = false;
using (FileStream fs = new FileStream(filePath, FileMode.Truncate, FileAccess.ReadWrite))
using (TextWriter writer = new StreamWriter(fs))
{
writer.WriteLine(configJSON);
}
return true;
IsolatedStorageFile.FileExists(string path) works but StreamReader(string samePath) doesn't?
I have validated both paths are equal. I have no idea why the StreamReader explodes
List<ProjectObj> ret = new List<ProjectObj>();
IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication();
if (!file.DirectoryExists("/projects/")) //trying to validate the dir exists
return ret;
string[] fileNames = file.GetFileNames("/projects/");
foreach (string filename in fileNames)
{
if (!file.FileExists("/projects/" + filename)) //validate just one more time..
continue;
ProjectObj tempProj = new ProjectObj();
//Even with the validation it still breaks right here with the bellow error
StreamReader reader = new StreamReader("/projects/"+filename);
An exception of type 'System.IO.DirectoryNotFoundException' occurred
in mscorlib.ni.dll but was not handled in user code
Message:Could not find a part of the path
'C:\projects\Title_939931883.txt'.
Path is not same in both cases. In first case you are getting User store for application and then search for file in it. But in later case you are simply searching in base directory.
StreamReader constructor expects absolute path of file.
You need to create IsolatedStorageFileStream and pass it on to StreamReader -
using (IsolatedStorageFileStream fileStream = new IsolatedStorageFileStream
("/projects/" + filename, FileMode.Open, file))
{
using (StreamReader reader = new StreamReader(fileStream ))
{
}
}
Give this one a try. Reading and writing files in IsolatedStorage has a different path and should be used like that. You should consider reading How to: Read and Write to Files in Isolated Storage.
public static List<ProjectObj> getProjectsList()
{
List<ProjectObj> ret = new List<ProjectObj>();
IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication();
if (!file.DirectoryExists("/projects/")) //trying to validate the dir exists
return ret;
string[] fileNames = file.GetFileNames("/projects/");
foreach (string filename in fileNames)
{
if (!file.FileExists("/projects/" + filename)) //validate just one more time...
continue;
ProjectObj tempProj = new ProjectObj();
using (var isoStream = new IsolatedStorageFileStream("/projects/" + filename, FileMode.Open, FileAccess.Read, FileShare.Read, file))
{
using (StreamReader reader = new StreamReader(isoStream))
{
}
}
This was the solution I came up with
List<ProjectObj> ret = new List<ProjectObj>();
IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication();
if (!file.DirectoryExists("/projects/"))
return ret;
foreach (String filename in file.GetFileNames("/projects/"))
{
IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile("/projects/"+filename, FileMode.Open, FileAccess.Read);
using (StreamReader reader = new StreamReader(fileStream))
{
String fileInfo = reader.ReadToEnd();
}
}
I don't know why I was getting the illegal operation on boot of app but I know why it was happening later on. I guess when you try and access the same file to quickly it causes errors. So I added in a fileshare and also I made sure to dispose of other accesses before this ran.
I'm trying to read a file entirely to a String variable.
I did this:
String text;
using (var store = IsolatedStorageFile.GetUserStoreForApplication())
using (var readStream = new IsolatedStorageFileStream("k.dat", FileMode.Open, store))
using (var reader = new StreamReader(readStream))
{
text= reader.ReadToEnd();
}
textBlock1.Text = text;`
It gave me a "Operation not permitted on IsolatedStorageFileStream" message from an IsolatedStorageException.
What am I doing wrong?
I tried by adding a .txt and .xml file in the file name, but it didn't work.
Where am I to put the file anyway? I tried
~\Visual Studio 2010\Projects\Parsing\Parsing\k.dat
I'm parsing it later using:
XmlReader reader = XmlReader.Create(new StringReader(xmldata));
flagLink = false;
while (reader.Read())
{
//and so on
Try with..
string text;
string filename="k.txt";
using (IsolatedStorageFile isolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
if (isolatedStorage.FileExists(fileName))
{
StreamReader reader = new StreamReader(new IsolatedStorageFileStream(fileName, FileMode.Open, isolatedStorage));
text = reader.ReadToEnd();
reader.Close();
}
if(!String.IsNullOrEmpty(text))
{
MessageBox.Show(text);
}
}
EDIT:
In case of xml,
try
{
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
IsolatedStorageFileStream isoFileStream = myIsolatedStorage.OpenFile("test.xml", FileMode.Open); //you can use your filename just like above code
using (StreamReader reader = new StreamReader(isoFileStream))
{
this.textbox1.Text = reader.ReadToEnd();
}
}
}
catch
{ }
This is the entire method and this worked fully:
String sFile = "k.dat";
IsolatedStorageFile myFile = IsolatedStorageFile.GetUserStoreForApplication();
//myFile.DeleteFile(sFile);
if (!myFile.FileExists(sFile))
{
IsolatedStorageFileStream dataFile = myFile.CreateFile(sFile);
dataFile.Close();
}
var resource = Application.GetResourceStream(new Uri(#"k.dat", UriKind.Relative));
StreamReader streamReader = new StreamReader(resource.Stream);
string rawData = streamReader.ReadToEnd();
return rawData;
I am using three xaml pages in one solution. I have created directory named as "Storage" in xaml1. I need to use the same directory across other two xaml....
code:
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
myIsolatedStorage.CreateDirectory("Storage");
IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile("Storage\\myFile.txt", FileMode.Open, FileAccess.Write);
using (StreamWriter writer = new StreamWriter(fileStream))
{ }
}
How to use this directory in other two xaml's?
Any help.....
Thanks in advance
just check if the directory exists, and then use the same code to work with it:
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
if(!myIsolatedStorage.DirectoryExists("Storage")) return;
if(!myIsolatedStorage.FileExists("Storage\\myFile.txt")) return;
var fileStream = myIsolatedStorage.OpenFile("Storage\\myFile.txt", FileMode.Open, FileAccess.Read);
using (StreamReader writer = new StreamReader(fileStream))
{ }
}
IsolatedStorage is one for the application, not for the page.
Some details and examples here.
I get the following on the second using(StreamWriter statement:
Value does not fall within the expected range.
#region save allowance
IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
//Open existing file
IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile("foo.txt", FileMode.Truncate, FileAccess.Write);
using (StreamWriter writer = new StreamWriter(fileStream))
{
writer.Write(App.ViewModel.Foo);
}
#endregion
#region save log
IsolatedStorageFileStream fileStream2 = myIsolatedStorage.OpenFile("log.txt", FileMode.Truncate, FileAccess.Write);
using (StreamWriter writer = new StreamWriter(fileStream))
{
foreach( var i in App.ViewModel.Items )
writer.Write(i.ToString());
}
#endregion
You're reusing fileStream the second time instead of fileStream2. By the way, to avoid this kind of mistake, you may want to wrap your filestream inside the using block.
#region save allowance
IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
//Open existing file
using (var writer = new StreamWriter(myIsolatedStorage.OpenFile("foo.txt", FileMode.Truncate, FileAccess.Write)))
{
writer.Write(App.ViewModel.Foo);
}
#endregion
#region save log
using (var writer = new StreamWriter(myIsolatedStorage.OpenFile("log.txt", FileMode.Truncate, FileAccess.Write)))
{
foreach( var i in App.ViewModel.Items )
writer.Write(i.ToString());
}
#endregion