Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am making an app based IO Concept .
1. First I have to Create New File and write some text in it.
2. Second I have made a button to read a file .
3. Next time when i start with this step . I should check it out that preciously any file exits or not . if exists then read and if not create new.


The 3rd thing in the introduction above is giving me problem . I am not getting how to write code for this . Please Help me if any body knows about it .

Read and Write codes i have developed and here it is
C#
private void btnWrite_Click(object sender, RoutedEventArgs e)
        {
       // Get the local folder.
    System.IO.IsolatedStorage.IsolatedStorageFile local =
        System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication();

            

    // Create a new folder named DataFolder.
    if (!local.DirectoryExists("DataFolder"))
        local.CreateDirectory("DataFolder");

    // Create a new file named DataFile.txt.
    using (var isoFileStream = 
            new System.IO.IsolatedStorage.IsolatedStorageFileStream(
                "DataFolder\\DataFile.txt", 
                System.IO.FileMode.OpenOrCreate,
                    local))
    {
        // Write the data from the textbox.
        using (var isoFileWriter = new System.IO.StreamWriter(isoFileStream))
        {
            isoFileWriter.WriteLine(this.textBox1.Text);
        }
    }

    // Update UI.
    this.btnWrite.IsEnabled = false;
    this.btnRead.IsEnabled = true;
}

            private void btnRead_Click(object sender, RoutedEventArgs e)
{
  
   // Obtain a virtual store for the application.
    System.IO.IsolatedStorage.IsolatedStorageFile local =
        System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication();

    // Specify the file path and options.
    using (var isoFileStream =
            new System.IO.IsolatedStorage.IsolatedStorageFileStream
                ("DataFolder\\DataFile.txt", System.IO.FileMode.Open, local))
    {
        // Read the data.
        using (var isoFileReader = new System.IO.StreamReader(isoFileStream))
        {
            this.textBlock1.Text = isoFileReader.ReadLine();
        }
    }
    // Update UI.
    this.btnWrite.IsEnabled = true;
    this.btnRead.IsEnabled = false;
  

  
}


Please Help as soon you can.
Posted
Comments
[no name] 29-Mar-14 19:26pm    
So use the FileExists method to check if the file exists. What is the problem?
himanshuchawla4393 30-Mar-14 4:26am    
I am not getting that logic itself .. That's y i have said for the help.. If you can then help me with the working code.
[no name] 30-Mar-14 5:17am    
Ah I see. Since that logic would be pretty much the same as the logic that you already have, I would guess that you just copied this code from somewhere without understanding anything about it and now just want someone to write your code for you. If you cannot read the documentation for the FileExists method and follow the example code presented to you then you either need to learn how to write code yourself or hire someone to write the code for you.
ZurdoDev 30-Mar-14 20:54pm    
It sounds pretty simple. Where are you stuck?
himanshuchawla4393 31-Mar-14 0:35am    
@InsertCleverUserName : Thanks for your answer . You should check first that the Code from where i have copied is not working and FileExists code there is not a working . Thanks for your reply and please you don't reply anymore and keep out of it.

1 solution

above code is fine you need to add a namespace "using System.IO;"
 
Share this answer
 
Comments
Christian Amado 2-Apr-14 16:58pm    
Is not necessary. He have System.IO defined inline.
ShubhamSaxena 2-Apr-14 17:01pm    
i was toking abt the code he pasted in one of his comment
Christian Amado 2-Apr-14 17:20pm    
So, answer the comment only.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900