none
How to write a file in silverlight??

    Question

  • Hello

    I want to write a file on click event of button using Page.xaml.cs.

    Pls tel me how diid i can do that..

     

    Thanks in advance...

    Monday, August 04, 2008 6:04 AM

Answers

  •  Hi,

    You mean to Isolated Storage right

     

    using (IsolatedStorageFile isoFile = IsolatedStorageFile.GetUserStoreForApplication())
    {
    using (IsolatedStorageFileStream isoStream =
    new IsolatedStorageFileStream("test.txt", FileMode.Create, isoFile))
    {
    using (StreamWriter sw = new StreamWriter(isoStream))
    {
    sw.Write("This is a dummy text from silverlight...");
    }
    }

    Monday, August 04, 2008 6:08 AM
  • hi roomy,
                  I will suggest you to go thru this msdn link

    http://msdn.microsoft.com/en-us/library/system.io.isolatedstorage.isolatedstoragefile(VS.95).aspx


     http://silverlight.net/forums/p/15910/52667.aspx#52667

     

    Monday, August 04, 2008 7:09 AM

All replies

  •  Hi,

    You mean to Isolated Storage right

     

    using (IsolatedStorageFile isoFile = IsolatedStorageFile.GetUserStoreForApplication())
    {
    using (IsolatedStorageFileStream isoStream =
    new IsolatedStorageFileStream("test.txt", FileMode.Create, isoFile))
    {
    using (StreamWriter sw = new StreamWriter(isoStream))
    {
    sw.Write("This is a dummy text from silverlight...");
    }
    }

    Monday, August 04, 2008 6:08 AM
  • I want to write code my self  in test.txt.

    like for any selected component in me web page i wanna towrite code.

    Will ur code do the same.

    Not getting which code wil be writte in tet.txt with ur code????

    Monday, August 04, 2008 6:28 AM
  • I'm not clear what you want. but as far as i understand you want to write to a file on Click of a button in silverlight and the file is located on the server.

    If this is case, You can write a service which accepts the data which you want to write as a parameter and call that service method on button click in silverlight.

    Correct me if im wrong. 

    Monday, August 04, 2008 6:36 AM
  •  Hi,

    If i am right. what you had ask me in your previous post is that ?

    You want to know which line of my above code write into the file"Text.txt" then 

    sw.Write("This is a dummy text from silverlight...");


    Monday, August 04, 2008 6:37 AM
  • Could you pls tel me which naespace to be added for using isloatedstorage file.

    As i have written your code in some functions body and it is giving error in isloatedStorage file regarding namespace.

    Monday, August 04, 2008 6:42 AM
  • No i don't wan to write on file that is located o server. It will be any local file and i want to write code on it on click event.

    File wil be creatde at the sane time.

    Monday, August 04, 2008 6:44 AM
  • hi roomy,
                  I will suggest you to go thru this msdn link

    http://msdn.microsoft.com/en-us/library/system.io.isolatedstorage.isolatedstoragefile(VS.95).aspx


     http://silverlight.net/forums/p/15910/52667.aspx#52667

     

    Monday, August 04, 2008 7:09 AM
  •  Hi Roomy,

    Please see the link by Rajesh.They will help you a lot. The file will be not created at server. In Silverlight, there is no server side event.
     

    Monday, August 04, 2008 7:15 AM
  • I have tried the code in the link given but i havn't made nay directory or subdirectory.

    When i execute the program the file doesn't created but it also not showing any error also.

    I have pasted code below. Pls let me know where actually the problem is occuring.

    private void savechannelfun(object sender, RoutedEventArgs e)
    {
    using (var store = IsolatedStorageFile.GetUserStoreForApplication())
    {
    StringBuilder sb = new StringBuilder();
    store.CreateFile("MyApp1.txt");
    string filePath = "MyApp1.txt";

    if (store.FileExists(filePath))
    {
    try
    {
    using (StreamWriter sw = new StreamWriter(store.OpenFile(filePath, FileMode.Open,FileAccess.Write)))
    {
    sw.WriteLine("To do list:");
    sw.WriteLine("1. Buy supplies.");
    }
    }
    catch (IsolatedStorageException ex)
    {

    sb.AppendLine(ex.Message);
    }
    }
    }
    }
    Monday, August 04, 2008 8:10 AM
  • The file shud be in root of your application. Check the file from windows explorer.

    Monday, August 04, 2008 8:42 AM
  • I have serached for file but its not there..

    Can you pls tel me the exact code or pls check out my code.

     

     

    Monday, August 04, 2008 8:52 AM
  • Helo i have found the file but its being created somewhere very deeply in C drive.

     Cann't i give my project path for the file????

    Monday, August 04, 2008 9:06 AM
  • it will create the file at user profile folder.

    C:\Documents and Settings\dharmesh.surti\Local Settings\Application Data - there will be some junk named folders. U can search file name over there 

    Monday, August 04, 2008 9:14 AM
  • Hi,

    If you want to create the file in the server, then you can use the following:

    // Path is in the System.IO namespace
    string filePath = Path.GetFullPath(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "myDirName"));

    if(!Directory.Exists(filePath))
    {
          Directory.CreateDirectory(filePath);
    }

    if(!File.Exists(filePath + "/myFilename.txt"))
    {
         File.Create(filePath + "/myFilename.txt"); // or, the logic you want to create the file using stream
    }

     

     

    Monday, August 04, 2008 9:15 AM
  • Can you check creating directory and then adding file to it. The msdn documentation says that the files are created in your app.

     

    Monday, August 04, 2008 9:16 AM
  • Hello

    i can surely go for that but problem persist in "Path.Combine" Combine is not shown in intelligence box.

    Can you pls tel me if any namespace to be added for that???

    Tuesday, August 05, 2008 1:12 AM
  • Its in System.IO.

     

    Tuesday, August 05, 2008 1:14 AM
  • here is the msdn link for path.combine


    Tuesday, August 05, 2008 1:16 AM
  • If you're trying to write a file in a different folder than it is writing to now, this is impossible. You have no access to other folder than the Isolated Storage due to security issues.

    btw the correct MSDN link to path.combine is: http://msdn.microsoft.com/en-us/library/system.io.path.combine.aspx

    Tuesday, August 05, 2008 1:30 AM
  • Page is not displaying here.Message is provided "Unable to provide service".

    Tuesday, August 05, 2008 1:31 AM
  •  You can use Isolated storage for this purpose

     Writing to a Isolated storage file

    using (IsolatedStorageFile isoFile = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    using (IsolatedStorageFileStream isoStream =
                        new IsolatedStorageFileStream(strFilename, FileMode.Create, isoFile))
                    {
                        using (StreamWriter sw = new StreamWriter(isoStream))
                        {
                            sw.Write(strStoreValue);
                        }
                    }
                }

     

    Reading from a saved storage file

     

    using (IsolatedStorageFile isoFile = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    using (IsolatedStorageFileStream isoStream =
                        new IsolatedStorageFileStream(strFilename, FileMode.OpenOrCreate, isoFile))
                    {
                        using (StreamReader sw = new StreamReader(isoStream))
                        {
                            string value = sw.ReadToEnd();
                        }
                    }
                }

     

    Hope this helps

    Tuesday, August 05, 2008 1:33 AM
  • This is path where my directoy(MyDir) is being stored: 

    C:\Documents and Settings\User1\Local Settings\Application Data\Microsoft\Silverlight\is\tvvif02x.3y5\mke45xse.2z3\1\s\qvt3kfid1m3zvkvjtj1ijlbe2wutdjfslr0255zqlbutdn0bh2aaaeea\f\MyDir\SubDir\MyApp.txt

    Pls tel me if there is any way to save it to position of my choice/ or at location tat is easily accessible.

    Tuesday, August 05, 2008 2:02 AM
  •  You are working on a silverlight application, so when you are saving a file that will be on the client side. You are not a user of that machine so you willnot have the freedom to choose the location..

    Tuesday, August 05, 2008 2:08 AM
  • If you are using IsolatedStorage you dont have choice of mentioning its path. for each client the folder or file will be created in diff system folders.

    For each SL application running on client there will be one isolated storage area and those isolated storage areas do not interact with each other. Its like sandbox enviroment.

    If you want to save a file on the server in your app then consider using web service for that.

     

    Tuesday, August 05, 2008 2:18 AM