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
Solved! Go to Solution.
Solved by SENL1362. Go to Solution.
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); } }
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.