Author Topic: Can file be opened, one method  (Read 19246 times)

0 Members and 1 Guest are viewing this topic.

T.Willey

  • Needs a day job
  • Posts: 5251
Can file be opened, one method
« on: September 05, 2006, 01:47:40 PM »
Is there a better way to check if a file can be opened than this?  I couldn't find any other way to check.  This works.  It will return false if the file is read only, or is open already (at least with my testing with Acad dwgs it does).

Thanks in advance.
Code: [Select]
private bool CanBeOpened (string FullPath) {
bool CanOpen = false;
try {
using (FileStream fs = new FileStream (FullPath, FileMode.Open)) {
CanOpen = (fs.CanWrite);
}
}
catch {
}
if (CanOpen && ((File.GetAttributes (FullPath) & FileAttributes.ReadOnly) == 0)) {
return true;
}
else {
return false;
}
}
« Last Edit: September 05, 2006, 01:48:51 PM by T.Willey »
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

MickD

  • King Gator
  • Posts: 3636
  • (x-in)->[process]->(y-out) ... simples!
Re: Can file be opened, one method
« Reply #1 on: September 05, 2006, 06:06:01 PM »
how about something like

Code: [Select]
        private bool CanBeOpened(string FullPath)
        {
            FileStream fs = new FileStream(FullPath, FileMode.Open);
            if (fs.CanWrite && ((File.GetAttributes(FullPath) & FileAttributes.ReadOnly) == 0))
            {
                return true;
                // or do your stuff:
            }
            return false;
            // or raise an error:
        }

you could just paste the body of the method into your routine that is using the file (see comments) instead of creating a seperate function or if you still want to use a function, return the fs perhaps for use.
« Last Edit: September 05, 2006, 06:07:07 PM by MickD »
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.”
- J.R.R. Tolkien

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Can file be opened, one method
« Reply #2 on: September 05, 2006, 06:48:14 PM »
I had something like that before, but it would crash when it couldn't open up a file.  I don't know enough to use a correct way of doing it, but I think it had something to do with the FileSteam calls though.  I will try this though, because maybe I had something different.  Thanks Mick.

Edit:  Yours thows the error also.  Thanks for trying Mick.
« Last Edit: September 05, 2006, 06:50:58 PM by T.Willey »
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

MickD

  • King Gator
  • Posts: 3636
  • (x-in)->[process]->(y-out) ... simples!
Re: Can file be opened, one method
« Reply #3 on: September 05, 2006, 07:01:06 PM »
Ok, it was more of a design example than an answer as your solution worked, what is the error you are getting?

Don't forget too that your fs object you created goes out of scope once the function returms! As I said, returning the fs would probably be a better option.

AND we were both missing an '&' in this line -
(File.GetAttributes(FullPath) & FileAttributes.ReadOnly)

 :laugh:
« Last Edit: September 05, 2006, 07:02:55 PM by MickD »
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.”
- J.R.R. Tolkien

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Can file be opened, one method
« Reply #4 on: September 05, 2006, 07:10:54 PM »
Ok, it was more of a design example than an answer as your solution worked,
Oh, oops.   :-)

what is the error you are getting?
I can't tell.  It brings up a dialog box saying I don't have JIT installed, with only the options to debug (which never works for me) or continue.  So I hit continue, and the program seems to stop.

Don't forget too that your fs object you created goes out of scope once the function returms! As I said, returning the fs would probably be a better option.
I'm justing using the fs object to see if I can write to the drawing, as I open it with ObjectDBX.  I don't think (don't really know) you could use the fs object for what I'm doing, or planning on doing once I learn more, but I most certainly could be wrong.

AND we were both missing an '&' in this line -
(File.GetAttributes(FullPath) & FileAttributes.ReadOnly)

 :laugh:
Oops again.  I will get the hang of thins one day.

Thanks for helping.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

MickD

  • King Gator
  • Posts: 3636
  • (x-in)->[process]->(y-out) ... simples!
Re: Can file be opened, one method
« Reply #5 on: September 05, 2006, 07:18:37 PM »
surely ObjectDBX would have some way of telling you if you can read/write to a file??
The problem with your test is although you check to see if it's ok, what's to say that it's not ok by the time you go to actually write to the file, you really do need to 'have' the file to work on it without error. Unlikely I know but...

I haven't used dbx before but I'd imagine it would be like working directly on the database which has it's own ways of letting you know if it's available (ErrorStatus results returned for example).
I take it that you are using this as a stand alone app not associated with AutoCAD?
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.”
- J.R.R. Tolkien

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Can file be opened, one method
« Reply #6 on: September 05, 2006, 07:34:07 PM »
surely ObjectDBX would have some way of telling you if you can read/write to a file??
The problem with your test is although you check to see if it's ok, what's to say that it's not ok by the time you go to actually write to the file, you really do need to 'have' the file to work on it without error. Unlikely I know but...

I haven't used dbx before but I'd imagine it would be like working directly on the database which has it's own ways of letting you know if it's available (ErrorStatus results returned for example).

ObjectDBX is just a way to open a drawing without loading it into the editor, so it acts almost just like the database of any drawing.  I have tried to just open the DBX doc, but that didn't work either.  Now I open the drawing right after I test it, and do my changes to it then, and then test the next one if it can be opened.  Maybe this is just to big for me to have as my first attempt at a real project.  I mean I got it to work, but I'm sure it can be made to run faster/smoother/smarter... etc.  If that is the case, then I will just do as much research as I can, instead of asking so many questions.  I really do appreciate all the help I have been given, and I don't want to seem like a leach.

I take it that you are using this as a stand alone app not associated with AutoCAD?
Na, this will be called from within Acad.  I think you have to pay for a licence for 'RealDwg' to use ObjectDBX without Acad installed, and I'm sure my company won't pay for that since I'm just beginning, and they have real programmers who know what they are doing.  :-D

If you think it would help I can post the code, but its long (around 300 lines), and I don't think people want to read the whole thing to try and find the couple of lines that may make it faster, I know I don't when I'm trying to help someone with there code, that they wrote.  :-D

Thanks again for all the help, I really do appreciate it.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

MickD

  • King Gator
  • Posts: 3636
  • (x-in)->[process]->(y-out) ... simples!
Re: Can file be opened, one method
« Reply #7 on: September 05, 2006, 07:44:06 PM »
Quote
Na, this will be called from within Acad...
Mate!?!,
what are you doing with dbx then, you can open a db and alter it with the managed wrappers without using the editor!
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.”
- J.R.R. Tolkien

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Can file be opened, one method
« Reply #8 on: September 05, 2006, 07:57:40 PM »
Quote
Na, this will be called from within Acad...
Mate!?!,
what are you doing with dbx then, you can open a db and alter it with the managed wrappers without using the editor!
:oops:
I don't know how to do it.  I just knew it can be done with DBX because of my background with lisp.  I didn't know it could be done that way.  I guess I will look into doing it that way tomorrow when I have my Acad close to me.  Can you give me a starting point for my reseach tomorrow?
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Glenn R

  • Guest
Re: Can file be opened, one method
« Reply #9 on: September 05, 2006, 08:30:10 PM »
Sure can...hang a sec...

Glenn R

  • Guest
Re: Can file be opened, one method
« Reply #10 on: September 05, 2006, 08:36:02 PM »
The code below is from one of my templates for doing in-memory batch runs over drawings.
It is in the click event for the 'ok' button on the dialog:

Code: [Select]
foreach (string drawingName in listBoxDrgs.Items) {

try {

// Create a new pristine dbase...
using (Database db = new Database(false, true)) {

using (Transaction tr = db.TransactionManager.StartTransaction()) {

//...and read in the dwg...
db.ReadDwgFile(drawingName, System.IO.FileShare.Read, true, null);
ed.WriteMessage("\nSome prompt mojo...drawing: {0}", drawingName);

tr.Commit(); // Optional depending on what you're doing...
}

db.SaveAs(drawingName, DwgVersion.Current);

}// database goes out of scope here...

} catch (System.Exception ex) {
// Do something here with exception...
}

}//foreach

I suggest you study it (look up functions in the ARX and Windows docs) and ask questions here.
Pay particular attention to the constructor of the new Database...

Cheers,
Glenn.
« Last Edit: September 05, 2006, 08:38:16 PM by Glenn R »

MickD

  • King Gator
  • Posts: 3636
  • (x-in)->[process]->(y-out) ... simples!
Re: Can file be opened, one method
« Reply #11 on: September 05, 2006, 08:57:01 PM »
Thanks Glenn, I didn't have anything that tidy that would not take an hour to clean up and post :)

This is the best way Tim, you can create and edit complete drawings without ever opening the drg in the editor.
Have fun.
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.”
- J.R.R. Tolkien

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Can file be opened, one method
« Reply #12 on: September 05, 2006, 10:47:21 PM »
Thank you both!!!  I will study this tomorrow at work, when I have all the documentation.

Is there any need than for ObjectDBX with .Net applications, if you only would use it within Acad?
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Glenn R

  • Guest
Re: Can file be opened, one method
« Reply #13 on: September 05, 2006, 11:06:18 PM »
Let's get some terminology clear.

ObjectDBX (now RealDWG) is a set of C++ libraries available by license, that allows you to create stand-alone windows programs that read and write dwg WITHOUT Autocad being present.

What you're referring to here, is the COM typelibrary that comes with AutoCAD and needs AutoCAD to run - 2 different beasts.

In answer to your question, no there is no reason to use 'ObjectDBX COM' if you're using .NET (well almost none). You can create a new database and read a dwg into like I demonstrated above. This is markedly faster than using 'ObjectDBX COM' as well.

Hope this helps.

Cheers,
Glenn.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Can file be opened, one method
« Reply #14 on: September 05, 2006, 11:37:54 PM »
Let's get some terminology clear.

ObjectDBX (now RealDWG) is a set of C++ libraries available by license, that allows you to create stand-alone windows programs that read and write dwg WITHOUT Autocad being present.

What you're referring to here, is the COM typelibrary that comes with AutoCAD and needs AutoCAD to run - 2 different beasts.

In answer to your question, no there is no reason to use 'ObjectDBX COM' if you're using .NET (well almost none). You can create a new database and read a dwg into like I demonstrated above. This is markedly faster than using 'ObjectDBX COM' as well.

Hope this helps.

Cheers,
Glenn.
That does help a lot Glenn.  Thanks again.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.