Customer Service | Training | Contact Us
You are here: Home > User Forums > arcgis desktop discussion forums > Thread Replies

ArcGIS Desktop Discussion Forums

ArcGIS Desktop - ArcObjects General forum

Moving n MXD files at once   George Silva Nov 03, 2009
Re: Moving n MXD files at once   Saqib Mehmood Nov 03, 2009
Re: Moving n MXD files at once   George Silva Nov 03, 2009
Re: Moving n MXD files at once   Saqib Mehmood Nov 03, 2009
Re: Moving n MXD files at once   George Silva Nov 04, 2009
Report Inappropriate Content • Top • Print • Reply    
Subject Moving n MXD files at once 
Author George Silva 
Date Nov 03, 2009 
Message Hello guys. I constructed a class using C# to move a bunch of mxds to a new location, opening each one and saving it as in the new location.

Visual Studio keeps giving me a permission access error, altought i have permission to used the directory.

Heres the code:

Do you guys have any suggestions? 
 
public class MxdMegaMover
    {
        private String _rootFolder;
        private String _destinationFolder;
        private List<String> _mxdList;

        public String rootFolder
        {
            get { return this._rootFolder; }
        }

        public String destinationFolder
        {
            get { return this._destinationFolder; }

            set { this._destinationFolder = value; }
        }

        public List<String> mxdList
        {
            get { return _mxdList; }
        }

        public MxdMegaMover(String rootFolder,String destinationFolder)
        {
            this._rootFolder = rootFolder;
            this._destinationFolder = destinationFolder;
            this._mxdList = getMxdList();
        }

        public List<String> getMxdList()
        {
            List<String> tempList = new List<string>();
            DirectoryInfo di = new DirectoryInfo(this._rootFolder);
            FileInfo[] files = di.GetFiles("*.mxd",SearchOption.AllDirectories);
            foreach(FileInfo fi in files)
            {
                tempList.Add(fi.FullName);
            }
            return tempList;
        }

        public void moveMxd()
        {
            Type t = Type.GetTypeFromCLSID(typeof(AppRefClass).GUID);
            System.Object obj = Activator.CreateInstance(t);
            IApplication pApp = obj as IApplication;

            IMxDocument pDoc = (IMxDocument)pApp.Document;
            foreach (String documentPathname in this._mxdList)
            {
                pApp.OpenDocument(documentPathname); //this is where the error is thrown.
                pApp.SaveAsDocument(this._destinationFolder + "\\" + System.IO.Path.GetFileName(documentPathname), false);
                Console.WriteLine(documentPathname + " salvo em " + this._destinationFolder);
            }
        }
 
  George R. C. Silva
GIS Analyst
georger.silva at gmail.com 
   
Report Inappropriate Content • Top • Print • Reply    
Subject Re: Moving n MXD files at once 
Author Saqib Mehmood 
Date Nov 03, 2009 
Message Why are you opening the Map Document for copying/moving it to a new location. You may try the following. 
 
// To copy a file to another location and 
// overwrite the destination file if it already exists.
System.IO.File.Copy(sourceFile, destFile, true);

// To move a file or folder to a new location:
System.IO.File.Move(sourceFile, destinationFile);
 
  Saqib Mehmood
saqibqureshi@gmail.com 
   
Report Inappropriate Content • Top • Print • Reply    
Subject Re: Moving n MXD files at once 
Author George Silva 
Date Nov 03, 2009 
Message That is exactly the question. I need to open each document and save as so i can update links for each mxd and it's layers.

Thats the real deal in this. Not just copy, but a automated Save As, so i can do in batch. 
  George R. C. Silva
GIS Analyst
georger.silva at gmail.com 
   
Report Inappropriate Content • Top • Print • Reply    
Subject Re: Moving n MXD files at once 
Author Saqib Mehmood 
Date Nov 03, 2009 
Message OK. I have tried the following, it is working fine, give it a try. 
 
public void ChangeMXDDataSource(String sourceMxd, String targetMxd)
{
	IMapDocument doc = new MapDocumentClass();
	doc.Open(sourceMxd, null);
	
	// change some properties... 
	
	/**IMap map = doc.get_Map(0);
	IFeatureLayer layer = (IFeatureLayer) map.get_Layer(0);
	IDataLayer2 dataLayer = (IDataLayer2) layer;
	IDatasetName name = (IDatasetName) dataLayer.DataSourceName;
	IWorkspaceName workspace = name.WorkspaceName;
	IPropertySet conn = workspace.ConnectionProperties;
	conn.SetProperty("SERVER", "new-server-name");
		
	workspace.ConnectionProperties = conn ;
	
	dataLayer.Disconnect();
	dataLayer.Connect(null);
	
	IMxdContents contents = (IMxdContents) map; 
	
	doc.ReplaceContents( contents); **/
	doc.SaveAs(targetMxd, true, true);
	  
	doc.Close();
}
 
  Saqib Mehmood
saqibqureshi@gmail.com 
   
Report Inappropriate Content • Top • Print • Reply    
Subject Re: Moving n MXD files at once 
Author George Silva 
Date Nov 04, 2009 
Message Just by replacing the statement with the new ImapDocument interface made it work.

Thanks. 
  George R. C. Silva
GIS Analyst
georger.silva at gmail.com