0

I am having problems on overwriting a file's content from another file in my application's Resources. I tried using File.WriteAllBytes(path, Properties.Resources.MyResource), but it didn't work. I know the file is there, it will never be nonexistent. What I need to do is simply copy the resource to the specified path. My function that is supposed to work:

public void Swap(bool v) {
        byte[] file = Properties.Resources.Resource;
        if (v) {
            file = Properties.Resources.Resource;
        } else {
            file = Properties.Resources.Backup;
        }
        stateBox.IsChecked = v;
        string path = Process.GetProcessesByName("Process")[0].Modules[0].FileName.Replace("Process.exe", "") + "File.txt";
        MessageBox.Show(path);
        File.Create(path);
        File.WriteAllBytes(path, file);
    }
Kroltan
  • 4,812
  • 5
  • 34
  • 56
  • Try adjusting the write/modify privileges of the file in windows. If you are hosting this site on a server, you may need to give write/modify permission on the file to the "NETWORK SERVICE" user before you can modify it programmatically. – BumbleB2na Jul 13 '12 at 21:29
  • It's not running on a server, It's running on a average computer with Admin permissions, yet it doesn't seem to work. – Kroltan Jul 13 '12 at 21:46
  • See if the answer from this article helps. http://stackoverflow.com/questions/864140/write-file-from-assembly-resource-stream-to-disk – JimDel Jul 14 '12 at 00:25
  • It seems there's a problem getting the destination path, I edited the question so you can see my code. I saw this in some other thread, and yes, I'm 100% sure that there's only 1 process on that name running. – Kroltan Jul 14 '12 at 01:07
  • Are you trying to write to the process's application directory? – Mark Hall Jul 14 '12 at 01:12
  • I am trying to write to the folder where the process is located at, if that's what you mean. – Kroltan Jul 14 '12 at 01:13
  • You may be running into problems if you are running Vista or Windows 7 they do not like you writing to the program files directory. See this [link](http://www.hanselman.com/blog/VistasShowCompatibilityFilesAndTheScrumptiousWonderThatIsFileVirtualization.aspx) – Mark Hall Jul 14 '12 at 01:15
  • Erm, more problems, it doesn't even reach the `stateBox.IsChecked = v;` line. EDIT: Or maybe I'm just changing the wrong value. That's supposed to check the box when true, right? – Kroltan Jul 14 '12 at 01:54
  • The code stops in the `if (v) {`... – Kroltan Jul 14 '12 at 03:02

0 Answers0