locked
Problem in File Saving on my system local drive RRS feed

  • Question

  • Hello All,

    I am using that piece of code for saving xml file on that desired location but

    string path = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "myapplog.xml");
            if (File.Exists(path))
            {
              File.Delete(path);
            }
            StreamWriter writer = File.CreateText(path);
            writer.Write("myapplog.xml");
            writer.Close();

    andalso use this code


    var doc = new XDocument (
                    new XElement ("UserInfo",
                        new XElement ("Name", tbxName.Text),
                        new XElement ("WebSite", tbxWebSite.Text),
                        new XElement ("Email", tbxEmail.Text)
                    )
                );

    File.Create(strpath + "UserInfo.Xml");
          StreamWriter swirte = new StreamWriter(strpath, true);
          doc.Save(swirte);
          
    but i got the error File operation not permitted. Access to path '' is denied in Silverlight 4

    my aim to save the file on my local machine without using any file dialog box in SilverLight4

    Monday, August 9, 2010 5:23 AM

Answers

  • Hi.

    Since you don't want to use any file dialogs for this, you only have two options:

    a) Store your files in isolated storage. That'll work with a browser app too.

    b) Create an out of browser application and request elevated rights, then you're able to write anywhere in the local file system.

    HTH

    Monday, August 9, 2010 6:01 AM

All replies

  • Hi.

    Since you don't want to use any file dialogs for this, you only have two options:

    a) Store your files in isolated storage. That'll work with a browser app too.

    b) Create an out of browser application and request elevated rights, then you're able to write anywhere in the local file system.

    HTH

    Monday, August 9, 2010 6:01 AM
  • You need to use a save file dialog if you want to save a file outside of isolated storage.   

    Monday, August 9, 2010 6:04 AM
  • Hi

    Thnx for your reply but thier is arestriction in my project to don't save the  files in isolated storage

    otherwise i am already try it & it run's smoothly tell me if any solution aprt from this

    Monday, August 9, 2010 6:36 AM
  • Did you read my answer? The only other solution is:

    "b) Create an out of browser application and request elevated rights, then you're able to write anywhere in the local file system."

    Monday, August 9, 2010 7:55 AM
  • Hello,


    i am using that piece of code & also check the option of Out of browser application on my project


    private void SaveUserInfo()
            {
                var doc = new XDocument (
                    new XElement ("UserInfo",
                        new XElement ("Name", tbxName.Text),
                        new XElement ("WebSite", tbxWebSite.Text),
                        new XElement ("Email", tbxEmail.Text)
                    )
                );
       


          


          string strpath = @"C:\Pranay\";
          
          File.Create(strpath + "UserInfo.Xml");
          StreamWriter swirte = new StreamWriter(strpath, true);
          doc.Save(swirte);
          
                return;
            }

            private void btnSaveInfo_Click (object sender, RoutedEventArgs e)
            {
                SaveUserInfo ();

         
            }

    but the situation still remains same.I mean it genrate the error Message File operation not permitted. Access to path 'C:\Pranay\UserInfo.Xml' is denied.

    Monday, August 9, 2010 8:22 AM
  • Erm. Of course it's not enough to check that option (also don't forget the option "Require elevated trust when running outside the browser"). You also need to install the application (right-click and select "Install [YourApp] onto this computer"). Then it'll work.

    Monday, August 9, 2010 8:39 AM
  • Even with OOB and elevated trust, you can not save a File to an arbitrary folder such as "C:\Pranay". You only have right to the Special Folders listed in System.Environment.SpecialFolder.




    Monday, August 9, 2010 10:48 AM
  • I knew I had done this in one of my projects. It's true that you cannot use the Managed API to do that though (sorry that I didn't remember that part). But it's possible using ActiveX. Here's a stripped down example of what I did:

    using (dynamic fso = AutomationFactory.CreateObject("Scripting.FileSystemObject"))
    {
        dynamic file = fso.CreateTextFile("c:\\demo.txt", true);
        file.WriteLine("File creation in SL 4");
        file.Close();
    }


    Edit: actually that makes you wonder why access is restricted through the managed API when you can simply use COM interop to achieve the same. I consider that a glitch in the SL runtime - if I'm running with elevated rights, I should be able to write to any location I want, right?

    Monday, August 9, 2010 11:28 AM
  • hello

    I use same code which u gave me but situation still remains same.

    Tuesday, August 10, 2010 12:38 AM
  • Hi Erm,


    Thanks for your help my problem is resolved it work's perfectly after applying elvated trust on the application

    Tuesday, August 10, 2010 1:59 AM
  • Hi Ken,

    You gave the correct answer, but no one seems to notice :)

    Maybe this code example will let ppl understand that you can perfectly read & write to disk as long as you use the open and save file dialogs.

    Read:

                OpenFileDialog OFD = new OpenFileDialog();
                OFD.Filter = "All Files (*.*)|*.*|XML Files (*.xml)|*.xml";
                OFD.FilterIndex = 2;
                if (OFD.ShowDialog() == true)
                {
                    using (StreamReader SR = OFD.File.OpenText())
                    {
                        String XML = SR.ReadToEnd();
                        SR.Close();
                        LoadXML(XML); // this is just what I did with the XML, you can put you own code here.
                    }
                }


    Write:

                SaveFileDialog SFD = new SaveFileDialog();
                SFD.Filter = "All Files (*.*)|*.*|XML Files (*.xml)|*.xml";
                SFD.FilterIndex = 1;
                if (SFD.ShowDialog() == true)
                {
                    String FileName = SFD.SafeFileName;
                    using (StreamWriter SW = new StreamWriter(SFD.OpenFile()))
                    {
                        SW.Write(EC.ToXML()); // This is my data to write, put your own code here...
                        SW.Flush();
                        SW.Close();
                    }
                }


     

    Tuesday, September 28, 2010 4:21 AM
  • Hi Ken,

    You gave the correct answer, but no one seems to notice :)


    Well, not really, because the original poster's question said:


    my aim to save the file on my local machine without using any file dialog box in SilverLight4


    Tuesday, September 28, 2010 5:20 AM
  • You're totally right; I missed that part :(

    Tuesday, September 28, 2010 6:13 AM
  • if you want to save a file inside a folder or a special folder:

    then you must need to use SaveFile Dialog, its upto you if you wanna show the dialog or assign the file name without displaying SaveFile dialog.
    the code I'm recommending is a trick which use the functionality of SaveFile Dialog but by avoiding to show it:

    saveFileDialog1.FileName = PATH;
    doc.Save(saveFileDialog1.FileName);

    Where as I faced this problem I did it as I mentioned...

    Thursday, January 16, 2014 5:14 PM