0

I want to edit a xml file. I can open, edit and save (overwrite) the file on the local drive.

I can open and edit the file on the network drive. I also can save it to a new path, but I cant overwrite the file.

    Dim localPath As String = "C:\...\file.xml" 'works

    Dim networkPath As String = "Y:\...\file.xml"   'doesnt work
    Dim networkPath2 As String = "\\my.network.local\...\file.xml" 'doesnt work

    Dim doc As XElement
    doc = XElement.Load(networkPath)

    doc.Save(networkPath)

I get this exception, when I try to save: System.IO.IOException: "The request is not supported."

Thanks for your help.

Andi
  • 1
  • 1
  • Could you not process it locally and move/copy it back to the network? – Andrew Mortimer Dec 10 '20 at 08:00
  • Yes, that's what I will do if there is no solution to the problem. But I already have a quite big project in which I would have to change a lot. – Andi Dec 10 '20 at 09:02
  • Do you have write permission to the file? Are you an Admin? VS doesn't run automatically as an Admin. You have to right click the shortcut and select Run as Admin. If you are a admin the you need to use the dollar sign in the path name to give admin privilidge. – jdweng Dec 10 '20 at 10:35
  • I am admin and have write permissions on the drive. For example, I can overwrite a txt file normally. Run as admin and the dollar sign showed no difference. – Andi Dec 10 '20 at 11:22
  • The error happens on the save only? Can you go to the file on the network drive and open it with notepad and then save it? – dbasnett Dec 10 '20 at 14:34
  • There are different rights that might be used to overwrite a file, and depending your access rights one or the other may work. An overwrite is either a delete and create, or a truncate and write operation. If you try delete and create a file and delete access is denied -> does not work. If you try to truncate and write it a file and modify access is denied -> does not work. Probably best to make a central method that tries both methods. – Christoph Dec 10 '20 at 23:20
  • I tried it out and didn't experience any problems even writing back to the same network location I opened it from. Is there something special about the network location? – Christoph Dec 10 '20 at 23:40
  • @dbasnett open & save it with np works – Andi Dec 15 '20 at 12:37
  • @Christoph its the network of my company. I tried on the public network and on my private network, in which I have all permissions. – Andi Dec 15 '20 at 12:42
  • hm, it works, when I delete the file before saving it. – Andi Dec 15 '20 at 12:51

1 Answers1

0

Had the same problem with

...writeAllLines(...)

My solution:

  1. creat backup file of the original file (file.bak.xml)

  2. delete the original file

  3. save the new file

  4. success? yes -> delete the bak file; no-> get the bak file as original file

     If File.Exists(path) = False Then
    
         doc.Save(path)
    
     Else
             Dim temp As String = path & ".bak.xml"
             File.Move(path, temp)
    
             Try
                 doc.Save(path)
                 File.Delete(temp)
             Catch ex As Exception
                 File.Move(temp, path)               
             End Try
    
         End If
    
Andi
  • 1
  • 1