as you all know we simply use
Session["kurd"]="";
or
Cahce.Insert(...)
and its save data in memory;
its save data without serialize or any thing like that so we can simply save everything
but its not reliable way to save data;
so may you guys say you can save on database or file but in my case
it cant be done, because my data its not serializable, and i can
only save it in cache , session , viewdata,viewbage
,... because it dos not need to be serialize data first ; and because
of not reliability in session , cache ,... i want way that can save
data just like cache and session ,... without serialize data
anyone knows how we can do such thing (save data) in disk
i mean save data on hard drive just like session did on memory?
update :
the data type is instance of that class in picture that contain httpclient , httpclienthandeler,deviceinfo,...
update :
how wonderful it is that no one knows
how can i save data in hard drive just like what session did in memory
no question no serialize no bs, just save it very easy
Programmatically you can use ObjectDumper.
Or, if you are debugging, you can use a Visual Studio extension called Object Exporter
i solved this problem
HttpClient cant be serialize but
i found out that the only thing in HttpClient that we should save in this thread
is "CookieContainer"
so serlize and deserilze like this
var formatter = new SoapFormatter();
string file = "..\cookies.dat";
//
using (Stream s = File.Create (file))
formatter.Serialize(s, cookies);
//
CookieContainer retrievedCookies = null;
using (Stream s = File.OpenRead (file))
retrievedCookies = (CookieContainer) formatter.Deserialize(s);
so i serialize it and save it and then for new use i create new HttpClient and assign that CookieContainer to HttpClent.CookieContainer
this problem was for Instasharper
Related
I want to back up the data for today's date in an XML file every 10 minutes I managed to create the XML file, but I couldn't find how to save the newly received data without adding the same data to the existing file
Can I convert the file I created to dataSet with dataSet.ReadXml, add the new data I got from the query and convert it back to an XML file and save it? What method should I use?
String QueryString = "SELECT * FROM dbo.db_records WHERE DAY(datetime) = DAY(CURRENT_TIMESTAMP)";
public void run()
{
while (true)
{
try
{
Thread.Sleep(600000);
if (odbcConnection.State != ConnectionState.Open)
{
odbcConnection.Close();
odbcConnection.Open();
}
DataSet dataSet = new DataSet("XMLDB");
odbcDataAdapter.Fill(dataSet, "#ID");
if (File.Exists(Path))
{
}
else
{
using (FileStream fs = File.Create(Path))
{
dataSet.WriteXml(fs);
}
}
}
catch (Exception) { }
}
}
Xml is not a great format if you want to append data, since it uses tags that need to be closed. So you have a few options:
Save separate files
Since you seem to fetch data for the current day, just attach date-info to your file-name. When reading the data you may need to read all files in the folder fitting the pattern, and merge it.
Use a format that is trivial to append
If your data model is simple tabular data you may use a .csv file instead. You can add data to this using one of the File.Append methods.
Overwrite all data
Get the complete data you want to save each time, and overwrite any existing data. This is simple, but may be slow if you have lots of data. But if the database is small and grow slowly this might be perfectly fine.
Parse the existing data
You could read the existing file with Readxml as you suggest, and use DataSet.Merge to merge it with your new set before overwriting the existing file. This may also be slow, since it needs to process all the data. But it may put less load on the database than fetching all data from the database each time.
In any case, you might want to periodically save full backups, or have some other way to handle corrupt files. You should also have some way to test the backups. I would also consider using the backup options built into most database engines if that is an alternative.
I've implemented a file upload in ASP.Net MVC. I've created a view model that received the uploaded file as an HttpPostedFileWrapper and within the controller action I can save the file to disk.
However, I want to perform the actual save in service method which is in a class library that doesn't implement System.Web. I can't therefore pass the HttpPostedFileWrapper object to the service method.
Does anyone know how to achieve this, either by receiving the file as a different object or converting it to something else prior to passing it. The only way I can think of is to read the content of the file into a MemoryStream, and pass this along with the other parameters such as filename individually, but just wondered if there was a better way?
Thanks
The best approach would probably be retrieving the image data (as a byte[]) and the name of the image (as a string) and passing those along to your service, similar to the approach you mentioned :
public void UploadFile(HttpPostedFileWrapper file)
{
// Ensure a file is present
if(file != null)
{
// Store the file data
byte[] data = null;
// Read the file data into an array
using (var reader = new BinaryReader(file.InputStream))
{
data = reader.ReadBytes(file.ContentLength);
}
// Call your service here, passing along the data and file name
UploadFileViaService(file.FileName, data);
}
}
Since a byte[] and a string are very basic primatives, you should have no problem passing them to another service. A Stream is likely to work as well, but they can be prone to issues like being closed, whereas a byte[] will already have all of your content.
I'm working on an UWP application where a user can input data which is placed in a listview. All fine and dandy, but how can I save the user data to a separate file and load it the next time a user boots up the app?
I've tried to find a solution, but I had great difficulty to understand these code snippets and on how to apply these (since I'm fairly new to C# and App development). Would somebody like to explain how I can achieve the saving/loading of the data and explain what the code does?
Thanks in advance! :)
You can create a file like this:
StorageFile ageFile = await local.CreateFileAsync("Age.txt", CreationCollisionOption.FailIfExists);
I can read and write to a file like this:
StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;
var ageFile = await local.OpenStreamForReadAsync(#"Age.txt");
// Read the data.
using (StreamReader streamReader = new StreamReader(ageFile))
{
//Use like a normal streamReader
}
if you are trying to write, use OpenStreamForWriteAsync;
If I understood well, you have some kind of object structure that serves as a model for your ListView. When the application is started, you want to read a file where the data is present. When closing the application (or some other event) write the file with the changes done. Right?
1) When your application is loaded / closed (or upon modifications or some event of your choice), use the Windows.Storage API to read / write the text into the file.
2) If the data you want to write is just a liste of strings, you can save this as is in the file. If it is more complicated, I would recommend serializing it in JSON format. Use JSON.NET to serialize (object -> string) and deserialize (object <- string) the content of your file and object structure.
Product product = new Product();
product.Name = "Apple";
...
string json = JsonConvert.SerializeObject(product);
This problem is somewhat similar to this.
In my case, I have a text file. And since there is no content importer that works for text file, I have to write my own functions using stream readers. What I am trying to accomplish is to read from the text file, and set a few values accordingly into the options screen. I have added all necessary references, but using "../options.txt" as filepath does not work. Quite possibly the filepath is resolved to something else than Content folder. How do I then proceed with it?
Also, I am getting errors saying "attempt to access method (System.IO....ctor) failed". Is it that I am missing to add some other reference?
Does this have to work on the xBox 360? If so it's not possible to just use the filesystem like you can in Windows, you'll need to use a storage device. To be honest I would just use this method anyway so if you decide to take your game on to WP7 or 360 it'll just work.
There is a nice demo game that loads and saves data on this Microsoft page as well as a description of what it's doing
This is taken from the demo, it shows how to open a storage container and serialize some config data to it, the load operation is just as simple.
// Create the data to save.
SaveGameData data = new SaveGameData();
data.PlayerName = "Hiro";
data.AvatarPosition = new Vector2(360, 360);
data.Level = 11;
data.Score = 4200;
// Open a storage container.
IAsyncResult result =
device.BeginOpenContainer("StorageDemo", null, null);
// Wait for the WaitHandle to become signaled.
result.AsyncWaitHandle.WaitOne();
StorageContainer container = device.EndOpenContainer(result);
// Close the wait handle.
result.AsyncWaitHandle.Close();
string filename = "savegame.sav";
// Check to see whether the save exists.
if (container.FileExists(filename))
// Delete it so that we can create one fresh.
container.DeleteFile(filename);
// Create the file.
Stream stream = container.CreateFile(filename);
// Convert the object to XML data and put it in the stream.
XmlSerializer serializer = new XmlSerializer(typeof(SaveGameData));
serializer.Serialize(stream, data);
// Close the file.
stream.Close();
// Dispose the container, to commit changes.
container.Dispose();
I have a Web Application in which I am using asp:FileUpload Control to upload user XSD and reading its content(actually not uploading the XML Schema) using following Statement in a Click button function
using (StreamReader reader = new StreamReader(FileUploadControlName.FileContent))
this reader object is used in Click button function to read XMLSchema and thus I generate collection on basis of that.
I just wanted to know is there any way to use this StreamReader object again so that I can handle the manipulation on other control action in Web-Application.
Means can there be a way to write the reader into Memorystream and reuse the MemoryStream.
Sorry I am new to this.
You can reuse MemoryStream by setting the Position property to 0.
Example:
Stream s = new MemoryStream();
StreamReader sr = new StreamReader(s);
// later... after we read stuff
s.Position = 0;
And if you want to reuse the same object trough the application then you can use a Session variable. Then when you need to reuse it just set position to 0 again and read it with StreamReader.
By calling using the SteamReader will be disposed after the block ends, but not the stream itself. You can store the stream in a Session variable and reuse it like that, but I suggest you keep in mind to clear the Session variable. You can use MemoryStream or you can move the file to e TEMP location and store the file location in a Session variable. I would go with that option.