c# - Error: Help D: System.IO.IOException: The process cannot access the file because it is being used by another process -


code here ._.

    using system;     using system.collections.generic;     using system.linq;     using system.text;      namespace consoleapplication1 { class program {      static void main(string[] args)     {         system.io.streamwriter file = new system.io.streamwriter("c:\\users\\public\\usernames.txt");         file.writeline();         file.close();         int usertype = 0;         system.io.streamreader fileusername =             new system.io.streamreader("c:\\users\\public\\usernames.txt");         file.close();         string retrievedusername = fileusername.readtoend();         file.close();            console.writeline("please note prototype, passwords not hashed/encrypted ^_^");         console.writeline("welcome medata service! ver. 0.01 beta, made mechron");         console.writeline("please enter username below or type register register new account on device");         string loginusername = console.readline();       if (loginusername == retrievedusername)    {            console.writeline("welcome user!");            usertype = 1;    }       else       {           if (loginusername == "register")           {                console.writeline("choose username!");               string registeredusername = console.readline();               system.io.streamwriter files = new system.io.streamwriter("c:\\users\\public\\usernames.txt");               file.writeline(registeredusername);               file.close();           }           else           {               console.writeline("error, command not recognized");}        }      }       } } 

my code above ^^ erm... well... i'm having trouble this.... system.io.ioexception: process cannot access file because being used process keeps popping when hit line system.io.streamwriter files = new system.io.streamwriter("c:\users\public\usernames.txt"); ;; can me? in advance!

the following code not close reader - closes writer again. if worked close reader before read.

system.io.streamreader fileusername = new system.io.streamreader("c:\\users\\public\\usernames.txt"); file.close(); string retrievedusername = fileusername.readtoend(); file.close(); 

you should make code this:

using (system.io.streamwriter file = new system.io.streamwriter("c:\\users\\public\\usernames.txt")) {     file.writeline(); }  int usertype = 0; string retrievedusername = string.empty;  using (system.io.streamreader fileusername = new system.io.streamreader("c:\\users\\public\\usernames.txt")) {     retrievedusername = fileusername.readtoend(); } 

the code follows flawed:

system.io.streamwriter files = new system.io.streamwriter("c:\\users\\public\\usernames.txt"); file.writeline(registeredusername); file.close(); 

see error? you're opening (and not closing!!) streamwriter called files, you're trying write , trying close file. big mistake.

this can fixed this:

using (system.io.streamwriter files = new system.io.streamwriter("c:\\users\\public\\usernames.txt")) {     files.writeline(registeredusername); } 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -