ani*_*lca 3 c# file-io file process
我的Windows服务中有一个方法如下:
System.IO.File.Copy(path, ConfigurationManager.AppSettings["BulkInsertGiftRegisterCreatorDirectory"] + System.IO.Path.GetFileName(path));
Loyalty.Entity.Base.FileInfo file = new Loyalty.Entity.Base.FileInfo();
file.FileName = path;
request.Object = file;
ResponseBase response = new ResponseBase(request);
RequestConnection connection = new RequestConnection("cn");
FileManager fileManager = new FileManager(request, connection);
response = fileManager.OfflineGiftRegisterBulkInsert();
System.IO.File.Delete(ConfigurationManager.AppSettings["BulkInsertGiftRegisterCreatorDirectory"] + System.IO.Path.GetFileName(path));
// here is the part of stored procedure that uses file
SELECT @SCRIPT= 'BULK INSERT GIFT_CARD.GIFT_TEMP'
+' FROM '''
+ @FILE_PATH
+''' WITH ('
+'FIELDTERMINATOR = '','','
+ 'KEEPNULLS'
+');'
Run Code Online (Sandbox Code Playgroud)
我可以手动从文件系统中删除该文件,但是这段代码告诉我"Ooops!System.IO.IOException:进程无法访问文件'filename',因为它正由另一个进程使用."
我在stackoverflow和其他地方搜索了类似的问题.但我找不到任何可以帮助我的东西.复制或删除方法返回void,我的代码中没有要处理的流.
我该如何解决?
提前致谢.
这是一种检查文件是否正在使用的方法:
public static System.Boolean FileInUse(System.String file)
{
try
{
if (!System.IO.File.Exists(file)) // The path might also be invalid.
{
return false;
}
using (System.IO.FileStream stream = new System.IO.FileStream(file, System.IO.FileMode.Open))
{
return false;
}
}
catch
{
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
另外,要等待我制作的文件:
public static void WaitForFile(System.String file)
{
// While the file is in use...
while (FileInUse(file)) ; // Do nothing.
}
Run Code Online (Sandbox Code Playgroud)
我希望这有帮助!