Solved

C# file unlock

Posted on 2014-02-17
6
834 Views
Last Modified: 2014-02-20
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
Comment
Question by:ukerandi
  • 4
  • 2
6 Comments
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 500 total points
ID: 39866090
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
 
LVL 10

Author Comment

by:ukerandi
ID: 39866100
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
 
LVL 10

Author Comment

by:ukerandi
ID: 39866122
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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
LVL 10

Author Comment

by:ukerandi
ID: 39866156
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
 
LVL 10

Author Comment

by:ukerandi
ID: 39866160
iam login as Administrator
0
 
LVL 40
ID: 39875774
Can you kill the process through the Task Manager (Ctrl-Alt-Del)?
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question