Question

This is the code:

static string ftpurl = "ftp://ftp.test.com/files/theme/";
static string filename = @"c:\temp\test.txt";
static string ftpusername = "un";
static string ftppassword = "ps";
static string value;

public static void test()
{
    try
    {
        FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(
        ftpurl + "/" + Path.GetFileName(filename));
        request.Method = WebRequestMethods.Ftp.UploadFile;

        request.Credentials = new NetworkCredential(ftpusername, ftppassword);

        StreamReader sourceStream = new StreamReader(@"c:\temp\test.txt");
        byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
        sourceStream.Close();
        request.ContentLength = fileContents.Length;

        Stream requestStream = request.GetRequestStream();
        requestStream.Write(fileContents, 0, fileContents.Length);
        requestStream.Close();

        FtpWebResponse response = (FtpWebResponse)request.GetResponse();

        Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);

        response.Close();
    }
    catch(Exception err)
    {
        string t = err.ToString();
    }
}

The exception is on the line:

StreamReader sourceStream = new StreamReader(@"c:\temp\test.txt");

Here is the exception:

The process cannot access the file 'c:\temp\test.txt' because it is being used by another process

System.IO.IOException was caught
  HResult=-2147024864
  Message=The process cannot access the file 'c:\temp\test.txt' because it is being used by another process.
  Source=mscorlib
  StackTrace:
       at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
       at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
       at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
       at System.IO.StreamReader..ctor(String path, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize, Boolean checkHost)
       at System.IO.StreamReader..ctor(String path)
       at ScrollLabelTest.FtpFileUploader.test() in e:\scrolllabel\ScrollLabel\ScrollLabel\FtpFileUploader.cs:line 33
  InnerException: 

Why I'm getting the exception and how can I fix it?

Was it helpful?

Solution

You should use a finally block, and close all the Streams there:

finally
{
    sourceStream.Close();
    requestStream.Close();
    response.Close();
}

This way even if you have an exception, everything will be closed.

This happends because maybe you have gotten an exception before the close of that file, and then, when you run your program again, and try to open, is still opened.

Close your file first, and then use the finally block, or the using statement.

Something like:

using (StreamReader reader = new StreamReader("file.txt"))
{
    line = reader.ReadLine();
}

I hope this helps

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top