• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1099
  • Last Modified:

C# file unlock

Hi,
Please see below Code
string Filepath = @"C:\\Program Files (x86)\\test.log";  

 if (File.Exists(Filepath))
            {
             System.IO.StreamReader file1 =new System.IO.StreamReader(Filepath);
            
           

            while ((line = file1.ReadLine()) != null)
            {
                xx.Add( line);
             
                counter++;
                
            }

Open in new window


please see below my error,Could you kindly someone help me to solve this issue.
thx
Error-File.png
0
ukerandi
Asked:
ukerandi
  • 4
  • 2
1 Solution
 
Jacques Bourgeois (James Burger)Commented:
The file has been opened by another application, where it might still be in use, or by a previous run of your own application, and has not been closed properly. Contrary to some other systems, .NET does not automatically closes files, you need to explicitely do it yourself.

If it is opened in another application, then you have to close that application and hope that it will close the file properly.

Otherwise, you first need to reboot your computer to release the file.

Be sure to add a file.Close call before the end of your method. Also, be sure to catch all possible errors in a try...catch and Close the file if an error happens after the file has been opened.
0
 
ukerandiAuthor Commented:
i can't stop that runining that application.thats the why iam getting stuck,

basicaly someone write Exe file creating logs file, i need to read that file, i can copy but its not allowed to do that. without copy another folder i have to read within same folder.

Can you help me with suggestion or any idea
0
 
ukerandiAuthor Commented:
or show me how to Kill the Process

like Process pr=new Process();

pr.Kill

I don't know where to put Exe file.thx
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
ukerandiAuthor Commented:
iam using following code
Process[] runningProcesses = Process.GetProcesses();
            foreach (Process process in runningProcesses)
            {
                // now check the modules of the process
                foreach (ProcessModule module in process.Modules)
                {
                    if (module.FileName.Equals("RecordsSr.exe"))
                    {
                        process.Kill();
                    }
                }
            }

Open in new window

But Access denied , can any one help me?
0
 
ukerandiAuthor Commented:
iam login as Administrator
0
 
Jacques Bourgeois (James Burger)Commented:
Can you kill the process through the Task Manager (Ctrl-Alt-Del)?
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now