Solved

C# file unlock

Posted on 2014-02-17
6
729 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 10

Author Comment

by:ukerandi
Comment Utility
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
Comment Utility
iam login as Administrator
0
 
LVL 40

Expert Comment

by:Jacques Bourgeois (James Burger)
Comment Utility
Can you kill the process through the Task Manager (Ctrl-Alt-Del)?
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

771 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now