Announcements
Due to scheduled maintenance, the Autodesk Community will be inaccessible from 10:00PM PDT on Oct 16th for approximately 1 hour. We appreciate your patience during this time.
.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Check for Read Only before DocumentManager.open

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
mordend
1013 Views, 2 Replies

Check for Read Only before DocumentManager.open

I know this has been asked but its never been fully answered form what I can find.

 

Answer I have found:

 

Document newDWG =  DocumentManager.open(FilePath);

 

newDWG.IsReadOnly will return if the previous open resulted in a readonly file. That is fine but it forces you to fully open the drawing only to find out its open on the network someplace else.

 

The solution I need is closer to the how CAD itself performs this. Checking for read only and returning the Username and PC name of who has it open. I cannot find a solution to produce a similar result. Even if I have to load the full document is there  a way to return the current user and or PC name?

 

thanks

2 REPLIES 2
Message 2 of 3
SENL1362
in reply to: mordend

I would first check for the file readonly attribute and then look for the Autodesk Lock File, as shown below.

To be on the save site use a proper XML parsing for decoding the DWL2 contents.

 

       [CommandMethod("TestDwgInUse")]
        public void TestDwgInUse()
        {
            Document doc = AcadApp.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;


            string pathname = @"C:\Temp";
            string dwgName = "TestDrawing";

            try
            {
                string dwgPathname = Path.Combine(pathname, dwgName + ".dwg");
                string dwlPathname = Path.Combine(pathname, dwgName + ".dwl");
                string dwl2Pathname = Path.Combine(pathname, dwgName + ".dwl2"); //XML formatted

                if (!File.Exists(dwgPathname))
                    throw new System.Exception(string.Format("File not found: {0}", dwgPathname));
                var fileAttributes = File.GetAttributes(dwgPathname);
                if (fileAttributes == FileAttributes.ReadOnly)
                    throw new System.Exception(string.Format("File ReadOnly: {0}", dwgPathname));

                if (File.Exists(dwl2Pathname))
                {
                    ed.WriteMessage("\n {0}", Path.GetFileName(dwgPathname));

                    //Becareful: the file will be in use, so File.ReadXXX will not work
                    using (FileStream stream = File.Open(dwl2Pathname, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                    {
                        //Use a XML parser or do it the simple way
                        using (StreamReader reader = new StreamReader(stream))
                        {
                            while (!reader.EndOfStream)
                            {
                                    //<?xml version="1.0" encoding="UTF-8">
                                    //<whprops>
                                    //<username>TheUserName</username>
                                    //<machinename>ThePCName </machinename>
                                    //<fullname></fullname>
                                    //<datetime>TheDate</datetime>
                                    //</whprops>
                                var dwl2Line = reader.ReadLine();
                                var tmpDwl2Line = dwl2Line.Split(new char[] { '<', '>' });
                                if (tmpDwl2Line.Length > 4)
                                {
                                    switch (tmpDwl2Line[1].ToUpper())
                                    {
                                        case "USERNAME":
                                            ed.WriteMessage("\n\t Locked By: {0}", tmpDwl2Line[2]);
                                            break;
                                        case "MACHINENAME":
                                            ed.WriteMessage("\n\t Locked On: {0}", tmpDwl2Line[2]);
                                            break;
                                        case "DATETIME":
                                            ed.WriteMessage("\n\t Locked At: {0}", tmpDwl2Line[2]);
                                            break;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (System.Exception ex)
            {
                ed.WriteMessage(ex.Message);
            }
        }

 

Message 3 of 3
mordend
in reply to: mordend

o Man, not sure why I didnt think to check the lock file.

 

I'm going to blame it on yesterday being my last day before holidays.

 

thank you so much.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

AutoCAD Inside the Factory


Autodesk Design & Make Report