Every developer getting following errors frequently. i.e."The file could not be accessed."File "can not be deleted"
Recently i also faced the same problem in one of my project task where i worked with some of open pdf files.Generally we will get these type of errors ,due to the following reasons
1. File is opened already.
2. File is using by another process
Finally find out the following simple and great example in c#..
This example is using the try-catch blocks.
Check all files from the specified folder:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO; //Required for directory and file operations
namespace File_operations
{
class Program
{
static void Main(string[] args)
{
DirectoryInfo Dir = new DirectoryInfo("C:\\Applictions\\SampleFiles");\\ Folder Path
FileInfo[] filePaths = Dir.GetFiles("*.*");\\Get all the files
foreach (FileInfo file in filePaths)
{
if (IsFileOpen(file))
Console.WriteLine("File is opened");
else
Console.WriteLine("File is not opened");
}
Console.ReadLine();
}
public static bool IsFileOpen(FileInfo file)
{
FileStream stream = null;
try
{
stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None);
}
catch (IOException)
{
return true;//file is opened
}
finally
{
if (stream != null)
stream.Close();
}
return false;//file is not opened
}
}
}
Find out specified format of files:
Change the following lines of code in the above example to get specific format files from a folder.
FileInfo[] filePaths = Dir.GetFiles("*.pdf");//Get all the pdf files from the folder
FileInfo[] filePaths = Dir.GetFiles("*.doc");//Get all the word document files from the folder
FileInfo[] filePaths = Dir.GetFiles("*.jpg");//Get all the jpg image files from the folder
please feel free to leave comments...!
Recently i also faced the same problem in one of my project task where i worked with some of open pdf files.Generally we will get these type of errors ,due to the following reasons
1. File is opened already.
2. File is using by another process
Finally find out the following simple and great example in c#..
This example is using the try-catch blocks.
Check all files from the specified folder:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO; //Required for directory and file operations
namespace File_operations
{
class Program
{
static void Main(string[] args)
{
DirectoryInfo Dir = new DirectoryInfo("C:\\Applictions\\SampleFiles");\\ Folder Path
FileInfo[] filePaths = Dir.GetFiles("*.*");\\Get all the files
foreach (FileInfo file in filePaths)
{
if (IsFileOpen(file))
Console.WriteLine("File is opened");
else
Console.WriteLine("File is not opened");
}
Console.ReadLine();
}
public static bool IsFileOpen(FileInfo file)
{
FileStream stream = null;
try
{
stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None);
}
catch (IOException)
{
return true;//file is opened
}
finally
{
if (stream != null)
stream.Close();
}
return false;//file is not opened
}
}
}
Find out specified format of files:
Change the following lines of code in the above example to get specific format files from a folder.
FileInfo[] filePaths = Dir.GetFiles("*.pdf");//Get all the pdf files from the folder
FileInfo[] filePaths = Dir.GetFiles("*.doc");//Get all the word document files from the folder
FileInfo[] filePaths = Dir.GetFiles("*.jpg");//Get all the jpg image files from the folder
please feel free to leave comments...!
No comments:
Post a Comment