|
There is no 100% effective way to do this, other than opening trying to open the file in Word or Excel.
You can open the file using the normal file operations and look at the file header but you'll have to compare the header to the known headers of each file type you want to support.
But, that in no way means that you're really looking at a Word or Excel file.
|
|
|
|
|
Theoretically you could simply attempt to open it with all file-format-reading-"procedures" you have available and see if any of them work.
But I would suggest to simply not bother. Other applications also just tell you something like "invalid file format". You can't take care of every eventuality. There's probably other stuff related to your application where investing your time instead would pay off more.
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
ok , but to display an "invalid file format" message , I want to know how to detect that this file is invalid ? for example how to detect if a .doc file is not a real Microsoft Word file ?
|
|
|
|
|
You could use Microsoft Office Interop to try opening it - if it succeeds it's probably a valid Word doc.
The simplest example I know of is in C# but I'm sure you'll be able to read it:
http://www.dotnetperls.com/word[^]
Suggested Google search terms for further reading:
vb.net word interop
vb.net open word interop
codeproject vb.net word interop
As you might have already guessed, there's also interop-functionality for other Office applications like Excel, so you could replace "word" with "office" in those search terms if you're also interested to validate other Office file types.
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
Hello !
I want to give to my application the possibility to drag and drop some kind of files from windows explorer inside my application.
But before the Drag and Drop operation be completed , I want to detect some details :
1. The user is Dragging a files or folders ?
2. What is the number of Files being dragged ?
3. What is the type of files being dragged ( for example the files may be Word files , Excel files...etc , so how to find the type of each file being dragged )
Hoe can I find these information before the drag and drop process is completed ?
Thank you !
|
|
|
|
|
I think you really need to read up on Drag and Drop[^] first. A lot of what you're talking about already happens in a normal drag and drop operation.
You'll get a list of filepaths. Use the System.Io.Path class to figure out what those files/folders are.
|
|
|
|
|
Hello !
I have only 1 problem :
In the DragEnter Event ,I have an instruction like this :
.....
Dim ext = System.IO.Path.GetExtension(files(0))
If Not ( ext.Equals(".xlsx", StringComparison.CurrentCultureIgnoreCase) Or ext.Equals(".xls", StringComparison.CurrentCultureIgnoreCase)) Then
e.Effect = DragDropEffects.None
Return
End If
.....
The problem is that with this code , I'm expecting that all the drag operation that not contain .xls or .xlsx will be cancelled .
But I have a strange behavior : All the other files that doesn't have .xls or .xlsx are blocked to drag-drop , except the .txt files. The files with .txt extensions will still be drag drop.
Why ?
Thank you !
|
|
|
|
|
Well, that's not all the code in the DragEnter event.
And you're only checking the first filename extension with files(0) .
|
|
|
|
|
Hello !
I'm creating an application in VB.net 2013 , with entity Framework 6 and SQL server 2008R2.
I have noticed that every query that run for the first time ( against a specific entity ) , is too slow comparing with other queries.
For example :
Dim query= ( For t in context.Mytable1 where t.nr>5 select t).Tolist --------- Read only 1 records - 25 seconds
Second execution
Dim query= ( For t in context.Mytable1 where t.nr<5 select t).Tolist --------- Read 400 records - 2 seconds
If I try to execute a query against another entity , it's the same scenario , the first time is too slow.
I have tried to use pre-generated views using a tool on NuGet , but there;s no difference.
What can I do ?
Thank you 1
modified 25-May-15 3:51am.
|
|
|
|
|
You are not comparing like for like, the two queries are different. The first includes a where clause, which means the database must read every record and test for that condition. The second query just extracts all records.
|
|
|
|
|
it's just a mistake on typing. I have corrected in my question.
I try 2 queries with different condition , just to show that even the second query read more records , it's faster.
Of course , If I do the same query the first and the second time , the second time is faster.
|
|
|
|
|
How does one query return only 1 record, but the same query run later returns 400? Or is that another typo? It may be that information is cached in the server, so it is found faster the second time.
|
|
|
|
|
The first query has the condition >5 , the second query has the condition <5.
It's not the same query.
|
|
|
|
|
Then you are still making different comparisons so your test is not valid.
|
|
|
|
|
Sorry but if you can see the 2 query are very similar , and different only by the condition.
Anyway , the general problem is this :
The first query executed against an entity is too slow , the others are faster.
The same thing when executing query for the first time against other entities.
|
|
|
|
|
Richard MacCutchan wrote: The first includes a where clause, which means the database must read every record and test for that condition.
Not if you have a reasonable index on the table.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
How did you measure it being "too slow"? Did you find a "faster" way?
It may be faster the second time for a number of reasons, like having an open and authenticated connection, having the table in cache, SQL Servers' optimizer, SQL Servers' memory being paged from disc, etc. Yes, with 401 records, SQL Server will have to look at all the records - but should be fast indeed once they are in the cache - depending on the column-definition, it may hold the entire table there.
Of that first query, how long is it really executing the task on SQL Server, and how does that compare to the total waiting time?
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
On top of what everyone else has said, Entity Framework itself has a "warm up time" with the very first query your app makes each time the app is launched. This is where Entity Framework is going through the entity classes and compares the structure against the database to make sure nothing changed. Obviously, this takes time.
If you really wanted to compare apples to apples, you have to get Entity Framework to make a different query first, one where you're going to throw the results away, just to get EF through its warm up time.
The only catch, EF6 has a pretty short warm up time, a few seconds for most models.
|
|
|
|
|
Ok , but how to force EF to do this "Warm Up Time" on another moment for example at program startup , because the user is better to wait some more time at program startup than when he need to do a specific job.
Yes , I can call a query , but what if the entities number is too high ?
|
|
|
|
|
I already told you how.
satc wrote: but what if the entities number is too high ?
It doesn't matter at all. You just have to execute a single query, ANY query against the database, even if it doesn't return any records. That is enough to get EF to do it's warm up work.
|
|
|
|
|
I think that's not true.
I wrote in my original question , that if I execute a query against a entity fro the first time , it need more time , second time it's faster.
But after if I try to execute another query against another entity for the first time , it's the same situation , this first query it's slower , and after the queries are faster.
And if at some point I do
context = new myentities
the situation is repeated like in the beginning.
|
|
|
|
|
I did not say that it was the cause of your problem.
All I said was Entity Framework has a warm up time to validate the database against the object model on the first query.
I have no idea what's causing your entire problem, other than probably bad indexing on the database. You have to setup any appropriate indexes on the database tables yourself. Entity Framework won't do it for you.
|
|
|
|
|
I've already done the indexing.
But anyway , I think a bad indexing would cause problems every time , not only the first time.
|
|
|
|
|
Dave Kreskowiak wrote: Entity Framework itself has a "warm up time" with the very first query Just another nail in the EF coffin. What a POS.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Not like I had experience with a lot of different "heavy weight" ORMs but I can't imagine how it could be any different unless you/it deliberately skip(s) schema validation. And I don't think it's a big deal given it's a one time thing. There are probably other things more worth complaining about
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|