c# - FileStream throws File not found -


the program meant set file path , idea when data set, should use function:

    public void sendfile(string filename, long filesize, networkstream io)     {         sendfilenametoserver();         sendfilesizetoserver();          byte[] filedata;          try         {             if (!file.exists(filename))             {                 throw new filenotfoundexception("file not exist!");             }             filestream openfilestream = new filestream(filename, filemode.open, fileaccess.read);             binaryreader breader = new binaryreader(openfilestream);              int32 remainingsize = convert.toint32(_filesize);                           {                 filedata = breader.readbytes(bufsize);                 io.write(filedata, 0, bufsize);                 remainingsize -= bufsize;             } while (remainingsize > bufsize);                          {                 filedata = breader.readbytes(remainingsize);                 io.write(filedata, 0, remainingsize);                 remainingsize -= remainingsize;             } while (remainingsize > 0);              breader.close();             openfilestream.flush();             openfilestream.close();             io.flush();             io.close();         }         catch (exception)         {             throw new exception();         }     } 

to send file given in file path server-side program receives file data.

the problem when gets line filestream openfilestream = new filestream(filename, filemode.open, fileaccess.read); tells me file not found. exception gives exception:thrown: "the process cannot access file 'd:\stepmania\songs\fragma\you alive\green.avi' because being used process." (system.io.ioexception) system.io.ioexception thrown: "the process cannot access file 'd:\*filepath*\green.avi' because being used process." time: 04-05-2013 21:11:39 thread:main thread[5532] can't think of process use file when stepmania not running. know file there because check file path , it's there should be. works fine if file in exact same folder program other that, can't seem find solution problem. have ideas wrong?

if need more of code, please tell me.

edit: server uses code receive file:

    public void receivefile(string filename, networkstream io)     {         // din egen kode         byte[] filedata = new byte[bufsize];          filestream writefilestream = new filestream(filename, filemode.create);         binarywriter bwrite = new binarywriter(writefilestream);          int bytesread = 0;         long remainingsize = convert.toint32(_filesize);                  {             console.writeline("remaining number of bytes: {0}", remainingsize);             bytesread = io.read(filedata, 0, bufsize); // read max 1000 bytes server via socket (actual value placed in "bytesread"             bwrite.write(filedata, 0, bytesread); // write received bytes file. number of received bytes placed in "bytesread"             remainingsize -= bytesread;         }         while (remainingsize > 0);          writefilestream.flush();         writefilestream.close();         bwrite.close();     } 

ok, found problem: server-side program interefered client-side program. here's fixed code sendfile code of client program:

public void sendfile(string filename, long filesize, networkstream io)     {         sendfilenametoserver();         sendfilesizetoserver();          byte[] filedata;          try         {             filestream openfilestream = file.openread(filename);             binaryreader breader = new binaryreader(openfilestream);              int32 remainingsize = convert.toint32(_filesize);                           {                 filedata = breader.readbytes(bufsize);                 io.write(filedata, 0, bufsize);                 remainingsize -= bufsize;             } while (remainingsize > bufsize);                          {                 filedata = breader.readbytes(remainingsize);                 io.write(filedata, 0, remainingsize);                 remainingsize -= remainingsize;             } while (remainingsize > 0);              openfilestream.flush();             breader.close();             openfilestream.close();             io.flush();             io.close();         }         catch (exception)         {             throw new exception();         }     } 

here's receivefile code server:

public void receivefile(string filename, networkstream io)     {         // din egen kode         byte[] filedata = new byte[bufsize];          filestream writefilestream = new filestream(lib.extractfilename(filename), filemode.create);         binarywriter bwrite = new binarywriter(writefilestream);          int bytesread = 0;         long remainingsize = convert.toint32(_filesize);                  {             console.writeline("remaining number of bytes: {0}", remainingsize);             bytesread = io.read(filedata, 0, bufsize); // read max 1000 bytes server via socket (actual value placed in "bytesread"             bwrite.write(filedata, 0, bytesread); // write received bytes file. number of received bytes placed in "bytesread"             remainingsize -= bytesread;         }         while (remainingsize > 0);          writefilestream.flush();         writefilestream.close();         bwrite.close();     } 

again, thank answered post!


Comments

Popular posts from this blog

c++ - Function signature as a function template parameter -

algorithm - What are some ways to combine a number of (potentially incompatible) sorted sub-sets of a total set into a (partial) ordering of the total set? -

How to call a javascript function after the page loads with a chrome extension? -