Pushkar Pushp

Pushkar Pushp

  • NA
  • 92
  • 5.3k

Create txt file and Upload file to server in console applica

Aug 27 2018 1:05 AM
This is my code :-
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
OleDbCommand command = new OleDbCommand(strSQL, connection);
try
{
string filename = "C:\\Users\\Pushkar\\Desktop\\Cp1dz_rpsmohangarden_" + DateTime.Now.ToString("yyyy-dd-MM-HH-mm-ss") + ".txt";
StreamWriter sw = new StreamWriter(filename); 
connection.Open();
using (OleDbDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
sw.WriteLine("{0}, {1}, {2}, {3}", reader["AttendanceDate"].ToString(), reader["EmployeeId"].ToString(), reader["InTime"].ToString(), reader["OutTime"].ToString()); 
}
}
if (File.Exists(filename)
{
String uriString = @"http://192.168.4.99/mzc/admin/AttendanceController/upload_attendance_sheet/";
WebClient myWebClient = new WebClient();
string fileName = filename;
Console.WriteLine("Uploading {0} to {1} ...", fileName, uriString);
byte[] responseArray = myWebClient.UploadFile(uriString, fileName);
Console.WriteLine("\nResponse Received.The contents of the file uploaded are:\n{0}", System.Text.Encoding.ASCII.GetString(responseArray));
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}    
 
 
 And i am getting error :-
 
 
 System.IO.IOException: The process cannot access the file 'C:\Users\Pushkar\Desktop\(generated txt file name)txt' because it is being used by another process.
 
 
 
 
Please Guide me 

Answers (5)