How to start process A after completing process B in c#?
up vote
0
down vote
favorite
I have written a windows service to move a file from one location to another and,
then read the file in the new location and write the data to the database.
In the middle of the execution there is an error message:
IOException: The process cannot access the file 'file path' because it is being used by another process.
My code:
File move class
public void mysql()
{
string fileName = "Bargstedt.csv";
string sourcePath = @"\192.168.1.2DataCompany Files";
string targetPath = @"C:Userssource";
string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
string destFile = System.IO.Path.Combine(targetPath, fileName);
if (!System.IO.Directory.Exists(targetPath))
{
System.IO.Directory.CreateDirectory(targetPath);
}
System.IO.File.Copy(sourceFile, destFile, true);
if (System.IO.Directory.Exists(sourcePath))
{
string files = System.IO.Directory.GetFiles(sourcePath);
fileName = System.IO.Path.GetFileName(fileName);
destFile = System.IO.Path.Combine(targetPath, fileName);
FileInfo info = new FileInfo(destFile);
bool exists = info.Exists;
if (exists == true)
{
try
{
int delay = 400;
File.Delete(@"C:UsersIsuruhsourceBargstedt.csv");
Thread.Sleep(delay);
System.IO.File.Copy(sourceFile, destFile, true);
Debug.WriteLine("file moved");
}
catch (IOException ex) { }
}
}
else
{
Console.WriteLine("Source path does not exist!");
}
Console.WriteLine("Press any key to exit.");
}
Insert Class
public void Insert()
{
if (this.OpenConnection() == true)
{
using(var reader = new StreamReader(@"C:UserssourceBargstedt.csv"))
//using (var stream = File.Open(path, FileMode.Open, FileAccess.Write, FileShare.ReadWrite))
{
List<string> listA = new List<string>();
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(',');
string querynew = "INSERT INTO new_jobs"
+ "(board_code,status,code,no1,no2,thickness,dimension,material,root,variable,number,stable,constant)"
+ "VALUES (?jobNo, ?strClientName, ?strClientReference, ?strJobCategory, ?datCommisioned, ?datPromisedDelivery, ?division, ?date_assigned, ?root, ?variable, ?number, ?stable, ?constant)";
I did not mention the whole code for the service since it's too lengthy.
As given in Solution 01 I tried to use,
using (var stream = File.Open(path, FileMode.Open, FileAccess.Write, FileShare.Read))
line instead of
using(var reader = new StreamReader(@"C:UserssourceBargstedt.csv"))
But that triggers errors in the below keywords I have used.
I am not sure is it possible to use a file open instead of a reader class?
Basically, what I need to do is start the Insert process after completing the file move process.
Any suggestions on how to do that?
PS: This is possibly a duplicate question of the question I have mentioned above. I tried my very best to make this question unique. Appreciate if not marked as duplicate. Thank you!
c# file-io resources windows-services
|
show 2 more comments
up vote
0
down vote
favorite
I have written a windows service to move a file from one location to another and,
then read the file in the new location and write the data to the database.
In the middle of the execution there is an error message:
IOException: The process cannot access the file 'file path' because it is being used by another process.
My code:
File move class
public void mysql()
{
string fileName = "Bargstedt.csv";
string sourcePath = @"\192.168.1.2DataCompany Files";
string targetPath = @"C:Userssource";
string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
string destFile = System.IO.Path.Combine(targetPath, fileName);
if (!System.IO.Directory.Exists(targetPath))
{
System.IO.Directory.CreateDirectory(targetPath);
}
System.IO.File.Copy(sourceFile, destFile, true);
if (System.IO.Directory.Exists(sourcePath))
{
string files = System.IO.Directory.GetFiles(sourcePath);
fileName = System.IO.Path.GetFileName(fileName);
destFile = System.IO.Path.Combine(targetPath, fileName);
FileInfo info = new FileInfo(destFile);
bool exists = info.Exists;
if (exists == true)
{
try
{
int delay = 400;
File.Delete(@"C:UsersIsuruhsourceBargstedt.csv");
Thread.Sleep(delay);
System.IO.File.Copy(sourceFile, destFile, true);
Debug.WriteLine("file moved");
}
catch (IOException ex) { }
}
}
else
{
Console.WriteLine("Source path does not exist!");
}
Console.WriteLine("Press any key to exit.");
}
Insert Class
public void Insert()
{
if (this.OpenConnection() == true)
{
using(var reader = new StreamReader(@"C:UserssourceBargstedt.csv"))
//using (var stream = File.Open(path, FileMode.Open, FileAccess.Write, FileShare.ReadWrite))
{
List<string> listA = new List<string>();
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(',');
string querynew = "INSERT INTO new_jobs"
+ "(board_code,status,code,no1,no2,thickness,dimension,material,root,variable,number,stable,constant)"
+ "VALUES (?jobNo, ?strClientName, ?strClientReference, ?strJobCategory, ?datCommisioned, ?datPromisedDelivery, ?division, ?date_assigned, ?root, ?variable, ?number, ?stable, ?constant)";
I did not mention the whole code for the service since it's too lengthy.
As given in Solution 01 I tried to use,
using (var stream = File.Open(path, FileMode.Open, FileAccess.Write, FileShare.Read))
line instead of
using(var reader = new StreamReader(@"C:UserssourceBargstedt.csv"))
But that triggers errors in the below keywords I have used.
I am not sure is it possible to use a file open instead of a reader class?
Basically, what I need to do is start the Insert process after completing the file move process.
Any suggestions on how to do that?
PS: This is possibly a duplicate question of the question I have mentioned above. I tried my very best to make this question unique. Appreciate if not marked as duplicate. Thank you!
c# file-io resources windows-services
2
You do not properly dispose of the FileHandle. Filehandles and network connections are the classical examples of unmanaged resources that need disposing. Ideally use the using block to get rid of them reliably.
– Christopher
Nov 22 at 2:36
@Adriano Repetti any thoughts?
– Simon
Nov 22 at 2:43
Where is the exception thrown? I'm going to guess that unstated part of this story is that there is another process that writes the file in the original location and that this service is supposed to grab it when it's done. Is that right?
– Tom Blodget
Nov 22 at 2:55
There ain't any other process to "write the file in the original location (in your words)". Only the file move class which move the csv from original location to the new location. That's it! @Tom Blodget
– Simon
Nov 22 at 3:02
the whole you first code sample can be replaced withFile.Move(sourceFile, destFile)
– vasily.sib
Nov 22 at 3:12
|
show 2 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have written a windows service to move a file from one location to another and,
then read the file in the new location and write the data to the database.
In the middle of the execution there is an error message:
IOException: The process cannot access the file 'file path' because it is being used by another process.
My code:
File move class
public void mysql()
{
string fileName = "Bargstedt.csv";
string sourcePath = @"\192.168.1.2DataCompany Files";
string targetPath = @"C:Userssource";
string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
string destFile = System.IO.Path.Combine(targetPath, fileName);
if (!System.IO.Directory.Exists(targetPath))
{
System.IO.Directory.CreateDirectory(targetPath);
}
System.IO.File.Copy(sourceFile, destFile, true);
if (System.IO.Directory.Exists(sourcePath))
{
string files = System.IO.Directory.GetFiles(sourcePath);
fileName = System.IO.Path.GetFileName(fileName);
destFile = System.IO.Path.Combine(targetPath, fileName);
FileInfo info = new FileInfo(destFile);
bool exists = info.Exists;
if (exists == true)
{
try
{
int delay = 400;
File.Delete(@"C:UsersIsuruhsourceBargstedt.csv");
Thread.Sleep(delay);
System.IO.File.Copy(sourceFile, destFile, true);
Debug.WriteLine("file moved");
}
catch (IOException ex) { }
}
}
else
{
Console.WriteLine("Source path does not exist!");
}
Console.WriteLine("Press any key to exit.");
}
Insert Class
public void Insert()
{
if (this.OpenConnection() == true)
{
using(var reader = new StreamReader(@"C:UserssourceBargstedt.csv"))
//using (var stream = File.Open(path, FileMode.Open, FileAccess.Write, FileShare.ReadWrite))
{
List<string> listA = new List<string>();
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(',');
string querynew = "INSERT INTO new_jobs"
+ "(board_code,status,code,no1,no2,thickness,dimension,material,root,variable,number,stable,constant)"
+ "VALUES (?jobNo, ?strClientName, ?strClientReference, ?strJobCategory, ?datCommisioned, ?datPromisedDelivery, ?division, ?date_assigned, ?root, ?variable, ?number, ?stable, ?constant)";
I did not mention the whole code for the service since it's too lengthy.
As given in Solution 01 I tried to use,
using (var stream = File.Open(path, FileMode.Open, FileAccess.Write, FileShare.Read))
line instead of
using(var reader = new StreamReader(@"C:UserssourceBargstedt.csv"))
But that triggers errors in the below keywords I have used.
I am not sure is it possible to use a file open instead of a reader class?
Basically, what I need to do is start the Insert process after completing the file move process.
Any suggestions on how to do that?
PS: This is possibly a duplicate question of the question I have mentioned above. I tried my very best to make this question unique. Appreciate if not marked as duplicate. Thank you!
c# file-io resources windows-services
I have written a windows service to move a file from one location to another and,
then read the file in the new location and write the data to the database.
In the middle of the execution there is an error message:
IOException: The process cannot access the file 'file path' because it is being used by another process.
My code:
File move class
public void mysql()
{
string fileName = "Bargstedt.csv";
string sourcePath = @"\192.168.1.2DataCompany Files";
string targetPath = @"C:Userssource";
string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
string destFile = System.IO.Path.Combine(targetPath, fileName);
if (!System.IO.Directory.Exists(targetPath))
{
System.IO.Directory.CreateDirectory(targetPath);
}
System.IO.File.Copy(sourceFile, destFile, true);
if (System.IO.Directory.Exists(sourcePath))
{
string files = System.IO.Directory.GetFiles(sourcePath);
fileName = System.IO.Path.GetFileName(fileName);
destFile = System.IO.Path.Combine(targetPath, fileName);
FileInfo info = new FileInfo(destFile);
bool exists = info.Exists;
if (exists == true)
{
try
{
int delay = 400;
File.Delete(@"C:UsersIsuruhsourceBargstedt.csv");
Thread.Sleep(delay);
System.IO.File.Copy(sourceFile, destFile, true);
Debug.WriteLine("file moved");
}
catch (IOException ex) { }
}
}
else
{
Console.WriteLine("Source path does not exist!");
}
Console.WriteLine("Press any key to exit.");
}
Insert Class
public void Insert()
{
if (this.OpenConnection() == true)
{
using(var reader = new StreamReader(@"C:UserssourceBargstedt.csv"))
//using (var stream = File.Open(path, FileMode.Open, FileAccess.Write, FileShare.ReadWrite))
{
List<string> listA = new List<string>();
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(',');
string querynew = "INSERT INTO new_jobs"
+ "(board_code,status,code,no1,no2,thickness,dimension,material,root,variable,number,stable,constant)"
+ "VALUES (?jobNo, ?strClientName, ?strClientReference, ?strJobCategory, ?datCommisioned, ?datPromisedDelivery, ?division, ?date_assigned, ?root, ?variable, ?number, ?stable, ?constant)";
I did not mention the whole code for the service since it's too lengthy.
As given in Solution 01 I tried to use,
using (var stream = File.Open(path, FileMode.Open, FileAccess.Write, FileShare.Read))
line instead of
using(var reader = new StreamReader(@"C:UserssourceBargstedt.csv"))
But that triggers errors in the below keywords I have used.
I am not sure is it possible to use a file open instead of a reader class?
Basically, what I need to do is start the Insert process after completing the file move process.
Any suggestions on how to do that?
PS: This is possibly a duplicate question of the question I have mentioned above. I tried my very best to make this question unique. Appreciate if not marked as duplicate. Thank you!
c# file-io resources windows-services
c# file-io resources windows-services
edited Nov 22 at 2:53
JohnB
1,3291015
1,3291015
asked Nov 22 at 2:32
Simon
6210
6210
2
You do not properly dispose of the FileHandle. Filehandles and network connections are the classical examples of unmanaged resources that need disposing. Ideally use the using block to get rid of them reliably.
– Christopher
Nov 22 at 2:36
@Adriano Repetti any thoughts?
– Simon
Nov 22 at 2:43
Where is the exception thrown? I'm going to guess that unstated part of this story is that there is another process that writes the file in the original location and that this service is supposed to grab it when it's done. Is that right?
– Tom Blodget
Nov 22 at 2:55
There ain't any other process to "write the file in the original location (in your words)". Only the file move class which move the csv from original location to the new location. That's it! @Tom Blodget
– Simon
Nov 22 at 3:02
the whole you first code sample can be replaced withFile.Move(sourceFile, destFile)
– vasily.sib
Nov 22 at 3:12
|
show 2 more comments
2
You do not properly dispose of the FileHandle. Filehandles and network connections are the classical examples of unmanaged resources that need disposing. Ideally use the using block to get rid of them reliably.
– Christopher
Nov 22 at 2:36
@Adriano Repetti any thoughts?
– Simon
Nov 22 at 2:43
Where is the exception thrown? I'm going to guess that unstated part of this story is that there is another process that writes the file in the original location and that this service is supposed to grab it when it's done. Is that right?
– Tom Blodget
Nov 22 at 2:55
There ain't any other process to "write the file in the original location (in your words)". Only the file move class which move the csv from original location to the new location. That's it! @Tom Blodget
– Simon
Nov 22 at 3:02
the whole you first code sample can be replaced withFile.Move(sourceFile, destFile)
– vasily.sib
Nov 22 at 3:12
2
2
You do not properly dispose of the FileHandle. Filehandles and network connections are the classical examples of unmanaged resources that need disposing. Ideally use the using block to get rid of them reliably.
– Christopher
Nov 22 at 2:36
You do not properly dispose of the FileHandle. Filehandles and network connections are the classical examples of unmanaged resources that need disposing. Ideally use the using block to get rid of them reliably.
– Christopher
Nov 22 at 2:36
@Adriano Repetti any thoughts?
– Simon
Nov 22 at 2:43
@Adriano Repetti any thoughts?
– Simon
Nov 22 at 2:43
Where is the exception thrown? I'm going to guess that unstated part of this story is that there is another process that writes the file in the original location and that this service is supposed to grab it when it's done. Is that right?
– Tom Blodget
Nov 22 at 2:55
Where is the exception thrown? I'm going to guess that unstated part of this story is that there is another process that writes the file in the original location and that this service is supposed to grab it when it's done. Is that right?
– Tom Blodget
Nov 22 at 2:55
There ain't any other process to "write the file in the original location (in your words)". Only the file move class which move the csv from original location to the new location. That's it! @Tom Blodget
– Simon
Nov 22 at 3:02
There ain't any other process to "write the file in the original location (in your words)". Only the file move class which move the csv from original location to the new location. That's it! @Tom Blodget
– Simon
Nov 22 at 3:02
the whole you first code sample can be replaced with
File.Move(sourceFile, destFile)
– vasily.sib
Nov 22 at 3:12
the whole you first code sample can be replaced with
File.Move(sourceFile, destFile)
– vasily.sib
Nov 22 at 3:12
|
show 2 more comments
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53423097%2fhow-to-start-process-a-after-completing-process-b-in-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
You do not properly dispose of the FileHandle. Filehandles and network connections are the classical examples of unmanaged resources that need disposing. Ideally use the using block to get rid of them reliably.
– Christopher
Nov 22 at 2:36
@Adriano Repetti any thoughts?
– Simon
Nov 22 at 2:43
Where is the exception thrown? I'm going to guess that unstated part of this story is that there is another process that writes the file in the original location and that this service is supposed to grab it when it's done. Is that right?
– Tom Blodget
Nov 22 at 2:55
There ain't any other process to "write the file in the original location (in your words)". Only the file move class which move the csv from original location to the new location. That's it! @Tom Blodget
– Simon
Nov 22 at 3:02
the whole you first code sample can be replaced with
File.Move(sourceFile, destFile)
– vasily.sib
Nov 22 at 3:12