|
Google results for "C# user control in VB6[^]" I know, "But I'm not using VB6!". You're not, but you ARE using a language that can consume COM classes.
|
|
|
|
|
Situation:
There's a text-file, say mytextfile.txt, and it's opened by a process.
Lets say that it's unknown which process locked the file.
How would I go about getting the process that locked said file.
So simply put, I want to get the process-name of the process that's locking mytextfile.txt.
I've seen this question posted all over the net, without people actually giving good answers.
What I see a lot, is people debating the validity of the question.
The validity of the question is not up for debate, the question and the answer to it, are.
So please, if you have any concrete advice or code samples, share them with me.
If you want to debate why I'm asking this question or the validity of my question, don't bother.
Because I'm certain there's a world of developers out there who would love to know the answer as well.
|
|
|
|
|
Some niche stuff is written here: Listing Used Files[^]
And yes, you may have to use some of those esoteric API calls with p/invoke.
The idea would be to enumerate all the processes first, and then enumerate resources locked by each process individually, while looking for a match.
Hope that helps. Let me know if you have troubles.
“Follow your bliss.” – Joseph Campbell
|
|
|
|
|
I'm very much inclined to agree with you.
My searches have led me to this page already.
I'm starting to believe I'll have to delve in to this in order to get what I need.
Perhaps WMI offeres an entry point to my dilemma?
I've only quickly glanced at C# in combination with WMI.
I've done WMI with VB6 before, but don't know how to go about it in C#...
|
|
|
|
|
Some obscure API call no doubt. You'll probably need to iterate through processes or something.
Here's an article with source code that does it, although it's C++, it may give you some pointers.
http://www.kartmann.org/freeware/WhoSLocking/ReadMe.htmRegards,
Rob Philpott.
|
|
|
|
|
C++ may give you some pointers?
|
|
|
|
|
Ta-Dah! Regards,
Rob Philpott.
|
|
|
|
|
Björn T.J.M. Spruit wrote: What I see a lot, is people debating the validity of the question.
The validity of the question is not up for debate, the question and the answer to it, are.
This is because most people don't understand what's going on. They just want to close that handle and don't think about what the impact would be on the application that has the file open, nor do they consider that they are possibly corrupting the contents of the file they are, themselves, trying to modify. It is very rarely ever a justifiable, nor supportable, course of action.
By "supportable", I mean that the outcome of the operation is not 100% predicatable and therefore not something you want to do in a production application.
|
|
|
|
|
Of course... there's always one that can't resist...
Fine, I'll indulge you:
I want to give someone the ability to see which process is locking a textfile.
There's a reason they want to know and I'm not inclined to debate their need to know.
Honestly... I don't care why they want to know.
They know not to go shutting down running processes willynilly, so that's not a concern.
Actually, whatever it is they want to do, from the point where they know which process is locking the file, isn't my concern.
What IS my concern, is to provide the very basic functionality of showing them which process is locking the file.
Lets hope this rant discourages anybody else that feels the need to debate the question or the validity of it.
Unbelievable that you didn't pick up the 'hint':
The validity of the question is not up for debate
Alrighty ?
|
|
|
|
|
Wowow relax, you could just ignore him you know
I also went out of my way to look for a solution, but it is indeed hard to find - I eventually found some highly obfuscated C code that is "supposed to" do what you want.. but the links given earlier are probably better anyway.
|
|
|
|
|
You'll have to excuse my snappy reaction before, but I want to shoot down a spin-off debate on this topic on 'the validity of it all' before it gets a chance to start.
I reckon you're right on the earlier links however.
Unless someone has some knowledge on my WMI + C# question I posted as a reaction to said post.
Fingers crossed.
|
|
|
|
|
I didn't have a problem with what your question was.
I was just saying why you see that discussion all over the web and noone really answering the question.
|
|
|
|
|
Fair enough.
And it's something I see more often.
But it's not up to others to decide the validity of the question.
A question was asked, why not answer it and then ask why someone is asking it, if one is truly interested?
I can understand why some would feel frustrated with the ivory tower some developers are hiding in.
The relevance of a question can only questioned by the one asking it, imo.
|
|
|
|
|
The reason there is always a debate on why this is a valid question, in my opinion, is that 90% of the time it is the programmers actual program that is holding onto the file by not disposing of its resources before trying to delete the file.
So when your users want to know what is locking a file, are they going to see that it is another part of your code?
|
|
|
|
|
I see your point.
Though, you'd get to the same point when a developer finds out, that the process it returns, is the one the developer is debugging or has built.
It's much more effective if a developer is forced to assume responsibility for oneself,with the shaming conclusion that the developer in question needs to kick one's own behind.
My point being, give what is asked for and engage in conversation as to what the context is.
It'll result in a more social event that way.
And as to answer your question, I think that what the user is going to see, is that they still have something like Ultra Edit/Notepad/etc... is still holding the file and they forgot to close the text editor before testing/running their application.
If the question is:"Is it likely to be something silly?", the answer is most likely yes.
In the end, however, that's besides the point.
I'd prefer they'd find out for themselves in an easy way, instead of them having to bother me.
I'm all for self-reliance.
|
|
|
|
|
Björn T.J.M. Spruit wrote: Though, you'd get to the same point when a developer finds out, that the process it returns, is the one the developer is debugging or has built.
Why have that developer (who can't figure out his own app locked a file) go through the pain of writing another app he doesn't understand just to find out he screwed up in his original app?
Björn T.J.M. Spruit wrote: I'd prefer they'd find out for themselves in an easy way, instead of them having to bother me.
Most of the people who are asking the question don't have a clue about what they're doing, let alone have the ability to teach themselves anything.
Björn T.J.M. Spruit wrote: I'm all for self-reliance
Björn T.J.M. Spruit wrote: I'm all for self-reliance.
So am I. Too bad this business has been overrun by idiots who couldn't teach themselves how to use Notepad let alone how get to Yahoo or Google and do any kind of research whatsoever.
|
|
|
|
|
That is why I stopped asking questions on the Internet. Too many people who don't have the answer feel inclined to respond without realizing all they are doing is wasting someone else's time. Of course, I am doing that right now since I don't have the answer either but I am at least self-aware making me an A**hole and not a moron.
|
|
|
|
|
Do you know what, ive pretty much stoped answering questions on the internet for exactly the opposite reason - people get uppity when you actually question why they are trying to do something one way when there is a reason we don't generally do it that way.
|
|
|
|
|
There is a difference between questioning a novice developers approach and questioning an experienced developers question.
|
|
|
|
|
And so the elitists decide whether they feel like helping or not.
Mostly deciding whether they deem it worthy of their time and effort.
Helping isn't performing an act in furthering yourself, it's an act of furthering others.
A concept, it seems, hard to grasp for some.
And yes, I'm not a novice.
And yes, my question is valid and I'll not have that debated.
So in the interest of furthering the one that asked a valid question, I gladly refer to the original post.
Any insights, other than the ones given, are welcome.
|
|
|
|
|
Hello Readers,
I am downloading a Html File using the following method.
<pre>public void LoadInitialData()
{
HttpWebRequest request = null;
try
{
Stopwatch sw = new Stopwatch();
RaiseStateChangedEvent(10, "Creating Connection to Server");
request = (HttpWebRequest)(WebRequest.Create(this._ServerUrl));
RaiseStateChangedEvent(20, "Connected to Server");
int timeout = request.Timeout;
if (request != null)
{
try
{
sw.Start();
request.BeginGetResponse(HttpRequestCallBackMethod, request);
sw.Stop();
RaiseStateChangedEvent(40, "Waiting for Data from Server --Time Taken: " + sw.ElapsedMilliseconds);
}
catch (Exception ex)
{
if (onErrorOccured != null)
onErrorOccured(ex.Message + "\n" + ex.StackTrace);
}
}
}
catch (Exception ex)
{
if (onErrorOccured != null)
onErrorOccured(ex.Message + "\n" + ex.StackTrace);
}
}
private void HttpRequestCallBackMethod(IAsyncResult iar)
{
RaiseStateChangedEvent(70, "Data Received From Server");
HttpWebRequest myHttpWebRequest = iar.AsyncState as HttpWebRequest;
HttpWebResponse response = (HttpWebResponse)myHttpWebRequest.EndGetResponse(iar);
using (StreamReader input = new StreamReader(response.GetResponseStream()))
{
string data = input.ReadToEnd();
RaiseStateChangedEvent(80, "Processing received Data");
ProcessData(data);
RaiseStateChangedEvent(100, "Completed Loading Initial Data");
}
}
private void RaiseStateChangedEvent(int progress, string message)
{
if (onStateChanged != null)
onStateChanged.Invoke(progress, message);
}
private void ProcessData(string data)
{
Console.WriteLine("Download Completed - Do any Processing here");
}</pre>
The program is written in .Net 2.0. When I try to run the same in Windows 7, I am not successful.
In Win7, Whenever I am calling this method continously (ie, 10 times in a for loop), I am not getting the replies for all the requests (the callback is not executed all time, only 5 or 6 responses I am getting). In 2000, Xp, Vista the program runs with no problem.
I have added a stopwatch to mesure the time it takes to invoke request.BeginGetResponse method. And the initial request is blocking the while application for 3 seconds (usually it should not. By calling teh LoadInitialData method in a seperate thread I managed the application from blocking).
Can anybody give some suggestions about this problem?
Thanks in advance.
|
|
|
|
|
Hello Readers,
I found the solution for the problem I mentioned.
Just add to lines of Code to modify the properties of request object.
Delay in the in the initial call is reduced by setting the Proxy as null. This happened in all versions of windows.
request.Proxy = null;
Also, In Windows 7, If I send 10 requests, I am only getting 5 or 6 responses. By setting KeepAlive property to false, I am getting all responses.
request.KeepAlive = true;
Hope this helps someone! Happy Coding...
|
|
|
|
|
Hi All,
I am trying to convert a class object into a string of xml format using XmlSerializer.
My class structure is like this:
namespace test
{
public class Class1
{
public Class1(){}
private string strStatus = "Active";
public string Status
{
get { return strStatus; }
set { strStatus = value;}
}
public Class2 Class2 { get; set; }
}
public class Class2
{
public Class2(){}
private string strName = "ABC";
public string Name
{
get { return strName; }
set { strName = value; }
}
public Class3 Class3 { get; set; }
}
public class Class3()
{
ArrayList Element;
private int intCount = 0;
public Class3()
{
Element = new ArrayList();
}
public int Count
{
get { return intCount;}
}
public void Add(Class4 Entry)
{
Element.Add(Entry);
intCount++;
}
public Class4 this[int index]
{
get
{
return (Class4)Element[index];
}
}
}
public class Class4
{
public Class4(){}
private int intSub1;
private int intSub2;
public int Sub1
{
get {return intSub1;}
set {intSub1 = value;}
}
public int Sub2
{
get {return intSub2;}
set {intSub2 = value;}
}
}
}
Now, i am using this code for converting object of Class1 to a string of xml format:
Class1 objClass1 = new Class1();
Class4 objClass4 = new Class4();
objClass4.Sub1 = 68;
objClass4.Sub2 = 73;
objClass1.Class2.Class3.Add(objClass4);
Class4 objClass4Dup = new Class4();
objClass4Dup.Sub1 = 76;
objClass4Dup.Sub2 = 65;
objClass1.Class2.Class3.Add(objClass4Dup);
System.Xml.Serialization.XmlSerializer x = new XmlSerializer(objClass1 .GetType());
StringWriter sw = new StringWriter();
x.Serialize(sw, objClass1);
string strxml = sw.ToString();
MessageBox.Show(strxml);
In the messagebox, i can see values till Class2 properly in xml format.
Then i can see the "Count" of Class3 and its value, but the values of Class4 are not coming into string.
Can anybody tell what i am missing or what i need to do to display values of Class4 into string?
Thanks,
Nagendra.
|
|
|
|
|
Message Closed
modified 23-Nov-14 6:39am.
|
|
|
|
|
Hi,thanks for replying.
I am using Indexer for getting the indexed based value of Element.
|
|
|
|
|