Solved

C# file unlock

Posted on 2014-02-17
6
935 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

728 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