Error Copying Files--Need help ASAP!
am 30.10.2007 20:50:00 von OriginalStealthI have a Windows app that has a C# class that calls a VB .Net module to copy
the latest files to the user's local drive from a shared location(via XML)
everytime the user hits the startup icon. When I compile in Release mode it
runs fine. When I run my new setup.exe, that runs fine. When I click the
app.exe from the startup menu, the copyfiles code is activated but I get the
following error.
Error Copying:\\shared drive\app.exe to C:\Program Files\Folder\app.exe
Message ->System.UnauthorizedAccessException: Access to the path
"C:\Folder\app.exe is denied.
at System.IO.__Error.WinIOError(Int32 error code, String str)
at System.IO.File.Delete(String
as BootStrapperUtility.BSUserValidation.CopyFiles(XmlDocument objXML)
//Startup.cs code
using System;
using System.Windows.Forms;
using FMRSystemsUtility;
namespace FMRSystems
{
///
/// Summary description for StartUp.
///
public class StartUp
{
public StartUp()
{
//
// TODO: Add constructor logic here
//;
}
[STAThread]
static void Main()
{
BootStrapperUtility.BSUserValidation oBootStrapper = new
BootStrapperUtility.BSUserValidation();
oBootStrapper.UserValidation();
// bool used to determine if we created a new mutex
bool createdNew;
System.Reflection.AssemblyName name = typeof(StartUp).Assembly.GetName();
string mutexName = name.CodeBase.Replace(@"\","/");
// Create a new mutex object with given name
System.Threading.Mutex m = new System.Threading.Mutex(true, mutexName,
out createdNew);
// If the mutex already exists then don't start program becase
// another instance is already running.
if ( !createdNew )
{
System.Windows.Forms.MessageBox.Show( "You are currently running the
application. ","Failure to launch Film Management Systems",
System.Windows.Forms.MessageBoxButtons.OK,System.Windows.For ms.MessageBoxIcon.Stop);
Environment.Exit( 1 );
return;
}
else
{
// BootStrapperUtility.BSUserValidation oBootStrapper = new
BootStrapperUtility.BSUserValidation();
// oBootStrapper.UserValidation();
// oBootStrapper=null;
frmSplash SplashWin = new frmSplash();
SplashWin.ShowDialog();
if( SplashWin.Connected)
{
//frmStartUp StartUpWin = new frmStartUp();
//StartUpWin.ulblStatus.Text = "Loading Contract Tracking
System.....";/
//StartUpWin.Show();
System.Windows.Forms.Application.Run(FMRSystems.frmMDIMain.I nstance);
//StartUpWin.Close();
}
// Release the mutex resources
m.ReleaseMutex();
}
}
}
}
//BootstrapperUtitlity.UserValidation Code
Imports System
Imports System.Data
Imports System.Data.OleDb
Imports System.Data.SqlClient
Imports Microsoft.Win32
Imports System.Xml
Imports System.Configuration.ConfigurationSettings
Namespace BootStrapperUtility
Public Class BSUserValidation
Private sWebserver As String = String.Empty
Private asCommandLineArgs As Array
Private sRunAs As String = System.Environment.UserName
Private bDontGetLocalFiles As Boolean = False
Private hFont As New Font("tahoma", 8, FontStyle.Regular)
Friend WithEvents lblVersion As System.Windows.Forms.Label
Friend WithEvents lblAppName As System.Windows.Forms.Label
Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
Private m_sLocalPath As String
Dim path As String = "\\twd-fs08\filmmgmt\_CTS\FMRSYS\"
Dim dirs As System.IO.DirectoryInfo() = New
System.IO.DirectoryInfo(path).GetDirectories()
Dim dtLatest As Date
Dim sLatestName As String = String.Empty
Dim sWebPaths As String = String.Empty
Dim frmCopyStatus As frmCopyStatus = New frmCopyStatus
'Dim ofmrsystems As New FMRSystems.StartUp
Public Sub UserValidation()
'Gives the hourglass to signify waiting to load
'Me.Cursor = Cursors.WaitCursor
frmCopyStatus.TopMost = True
'STEP 1 - Get the latest CTS build/CTS support files from the
network.
GetLatestCTSBuild()
'STEP 2 - (GUTCHECK) Check to see if the user has the Film
Management Report Systems Folder and the FMRSystems.exe file.
'This is just in case it was accidentally deleted. If one of
these folders is missing, launch a reinstall. At
'that time the user will be allowed to "Repair CTS" and the
program will exit. If both files are present
'continue on...
Dim sSetUpLocationAndFile As String
Dim sRemoteFile As String
Dim sExecutable As String
sSetUpLocationAndFile = "C:\Program Files\TBS\Film Management
Reporting Systems\FMRSystems.exe"
sRemoteFile = "\\twd-fs08\filmmgmt\_CTS\FMRSYS\" & sLatestName &
"\" & "Setup.exe"
'If Not System.IO.File.Exists(sSetUpLocationAndFile) Then
' System.Diagnostics.Process.Start(sRemoteFile,
sRunAs.ToLower())
' System.Windows.Forms.Application.DoEvents()
' System.Threading.Thread.Sleep(5400)
' System.Diagnostics.Process.Start(sSetUpLocationAndFile)
' ofmrsystems.Main()
' Exit Sub
'End If
'STEP 3 - Continue on....
Start()
frmCopyStatus.Close()
Dim strConn As String
strConn =
System.Configuration.ConfigurationSettings.AppSettings("Prod uction")
'ofmrsystems.Main()
End Sub
Public Function Start()
'frmCopyStatus.ProgressBarCopyFiles.Value = 1
frmCopyStatus.ProgressBarCopyFiles.Minimum = 1
frmCopyStatus.ProgressBarCopyFiles.Maximum = 55
frmCopyStatus.ProgressBarCopyFiles.Step = 1
frmCopyStatus.TopMost = True
frmCopyStatus.ucfstatus.Text = "Checking files..."
frmCopyStatus.Show()
frmCopyStatus.ProgressBarCopyFiles.PerformStep()
Dim sPathToManifest As String
Dim objHTTP As Net.HttpWebRequest
Dim objHTTPResponse As Net.WebResponse
Dim objStream As IO.StreamReader
Dim e As Exception
Dim objXML As Xml.XmlDocument
Dim sHomeDirectory As String
Dim objNode As Xml.XmlNode
Dim sFileToLaunch As String
asCommandLineArgs = System.Environment.GetCommandLineArgs
If asCommandLineArgs.GetUpperBound(0) > 0 Then
sWebserver = asCommandLineArgs(1).ToString
End If
sWebserver = "10.188.195.92" + sWebserver.ToUpper
System.Environment.GetEnvironmentVariables()
objXML = New Xml.XmlDocument
System.Windows.Forms.Application.DoEvents()
sHomeDirectory = "C:\Program Files\TBS\Film Management Reporting
Systems\" 'System.Environment.CurrentDirectory"
m_sLocalPath = sHomeDirectory
'
DisplayVersion()
If sWebserver.ToLower.Trim = "" Then
sWebserver = "appname"
End If
'
' the webserver is assumed to be without http at this point,
let's append it...
'
If Microsoft.VisualBasic.Right(sWebserver, 1) <> "/" Then
sPathToManifest = "http://" & sWebserver & "/Manifest.xml"
Else
sPathToManifest = "http://" & sWebserver & "Manifest.xml"
End If
'
' now that we have an url, let's connect to the manifest file on
the server...
'
Try
objHTTP = CType(Net.WebRequest.Create(sPathToManifest),
Net.HttpWebRequest)
objHTTP.PreAuthenticate = True
'
' set our credentials so we can come in
' as NT Challenge/Response (NTLM)
'
objHTTP.Credentials = Net.CredentialCache.DefaultCredentials
'
' get the response from the server..
'
frmCopyStatus.ucfstatus.Text = "Communicating with server..."
System.Windows.Forms.Application.DoEvents()
objHTTPResponse = objHTTP.GetResponse
'
' read the response into a stream...
'
objStream = New
IO.StreamReader(objHTTPResponse.GetResponseStream)
'
' read the xml manifest file...
'
frmCopyStatus.ucfstatus.Text = "Reading manifest file..."
System.Windows.Forms.Application.DoEvents()
objXML.LoadXml(objStream.ReadToEnd)
'
' ok, we've got our manifest loaded up, let's notify the user
' and start copying...
'
If Not bDontGetLocalFiles Then
CopyFiles(objXML)
End If
Catch e
MsgBox(e.ToString)
End Try
'Create an XML file to hold that data on the web server.
'Copy all files from the build folder to the web server.
End Function
Private Function CopyFiles(ByVal objXML As Xml.XmlDocument)
Dim i As Long
Dim objNode As Xml.XmlNode
Dim sLocalPath As String
Dim sRemotePath As String
Dim sFileName As String
Dim sDestination As String = String.Empty
Dim e As Exception
Dim regKey As RegistryKey
Dim sAllUsersStartMenu As String = String.Empty
'
' next, copy the executables, should just be one for now....
'
For i = 0 To
objXML.SelectSingleNode("Manifest/Executables").ChildNodes.C ount - 1
System.Windows.Forms.Application.DoEvents()
'
' do the executables
'
objNode =
objXML.SelectSingleNode("Manifest/Executables").ChildNodes.I tem(i)
sFileName = objNode.SelectSingleNode("Name").InnerText.Trim
sRemotePath =
objNode.SelectSingleNode("Location").InnerText.Trim & "FMRSYS" & "\" &
sLatestName & "\" & "Support Files\"
sDestination =
objNode.SelectSingleNode("Destination").InnerText.Trim
'TODO KB-FINISH THIS UP
Try
Select Case sDestination.ToUpper
Case "DESKTOP"
sLocalPath =
Environment.GetFolderPath(Environment.SpecialFolder.DesktopD irectory) & "\" &
sFileName
'MsgBox("sLocalPath=" + sLocalPath)
Case "ALLUSERSSTARTMENU"
regKey =
Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows \CurrentVersion\Explorer\User Shell Folders", False)
sAllUsersStartMenu =
DirectCast(regKey.GetValue("Common Programs", Nothing), String)
sLocalPath = sAllUsersStartMenu & "\AppName\" &
sFileName
'MsgBox("sLocalPath=" + sLocalPath)
Case Else
sLocalPath = "C:\Program Files\TBS\Film
Management Reporting Systems" & "\" & sFileName
'sLocalPath = m_sLocalPath & "\" & sFileName
End Select
Catch ex As Exception
'sLocalPath = m_sLocalPath & "\" & sFileName
End Try
If IsModifiedFile(sLocalPath, sRemotePath & sFileName) Then
Try
'
' the following will blow chunks if the file is in
use (the app is running).
' that's cool, FIDO...
'
'
' attempt to delete specified file...
'
frmCopyStatus.ucfstatus.Text = "Deleting: " &
sFileName
System.IO.File.Delete(sLocalPath)
System.Threading.Thread.Sleep(700)
System.Windows.Forms.Application.DoEvents()
frmCopyStatus.ucfstatus.Text = "Copying: " & sFileName
System.IO.File.Copy(sRemotePath & sFileName,
sLocalPath)
Catch e
MsgBox("Error Copying: " + sRemotePath + sFileName
+ " TO " + vbCrLf + sLocalPath + vbCrLf + vbCrLf + "Message -> " + e.ToString)
End Try
End If
System.Windows.Forms.Application.DoEvents()
frmCopyStatus.ProgressBarCopyFiles.PerformStep()
Next
For i = 0 To
objXML.SelectSingleNode("Manifest/ThirdParty").ChildNodes.Co unt - 1
System.Windows.Forms.Application.DoEvents()
'
' do the 3rd party crap second, this will be the
infragistics grid garbage...
'
objNode =
objXML.SelectSingleNode("Manifest/ThirdParty").ChildNodes.It em(i)
sFileName = objNode.SelectSingleNode("Name").InnerText.Trim
sRemotePath =
objNode.SelectSingleNode("Location").InnerText.Trim & "FMRSYS" & "\" &
sLatestName & "\" & "Support Files\"
sLocalPath = m_sLocalPath & "\" & sFileName
If IsModifiedFile(sLocalPath, sRemotePath & sFileName) Then
'
' attempt to delete/copy specified file...
'
Try
'
' the following will blow chunks if the file is in
use (the app is running).
' that's cool, FIDO...
'
frmCopyStatus.ucfstatus.Text = "Deleting: " &
sFileName
System.IO.File.Delete(sLocalPath)
System.Threading.Thread.Sleep(700) ' was 200
System.Windows.Forms.Application.DoEvents()
frmCopyStatus.ucfstatus.Text = "Copying: " & sFileName
System.IO.File.Copy(sRemotePath & sFileName,
sLocalPath)
Catch e
'MsgBox(e.ToString)
End Try
End If
System.Windows.Forms.Application.DoEvents()
frmCopyStatus.ProgressBarCopyFiles.PerformStep()
Next
System.Windows.Forms.Application.DoEvents()
'
' next, copy our assemblies
'
For i = 0 To
objXML.SelectSingleNode("Manifest/Assemblies").ChildNodes.Co unt - 1
System.Windows.Forms.Application.DoEvents()
'
' now to the assemblies...
'
'
objNode =
objXML.SelectSingleNode("Manifest/Assemblies").ChildNodes.It em(i)
sFileName = objNode.SelectSingleNode("Name").InnerText.Trim
sRemotePath =
objNode.SelectSingleNode("Location").InnerText.Trim & "FMRSYS" & "\" &
sLatestName & "\" & "Support Files\"
sLocalPath = m_sLocalPath & "\" & sFileName
If IsModifiedFile(sLocalPath, sRemotePath & sFileName) Then
Try
'
' attempt to delete specified file...
'
'
' the following will blow chunks if the file is in
use (the app is running).
' that's cool, FIDO...
'
frmCopyStatus.ucfstatus.Text = "Deleting: " &
sFileName
System.IO.File.Delete(sLocalPath)
System.Threading.Thread.Sleep(700)
System.Windows.Forms.Application.DoEvents()
frmCopyStatus.ucfstatus.Text = "Copying: " & sFileName
System.IO.File.Copy(sRemotePath & sFileName,
sLocalPath)
Catch e
End Try
End If
System.Windows.Forms.Application.DoEvents()
frmCopyStatus.ProgressBarCopyFiles.PerformStep()
Next
End Function
Private Function IsModifiedFile(ByVal sLocalPath As String, ByVal
sRemotePath As String) As Boolean
'
' to compare the file on the local machine and the one on the
server and
' see if they are different. returns true if the local file
cannot
' be found (i.e new install, or the user has accidentally
deleted it)
'
Dim bReturn As Boolean
Dim datLocalLastModified As DateTime
Dim datRemoteLastModified As DateTime
'
' First check and see if the local file exists...
'
If System.IO.File.Exists(sLocalPath) Then
'
' the file exists locally. let's see if it has been
modified on the server...
'
'
datLocalLastModified =
System.IO.File.GetLastWriteTime(sLocalPath)
datRemoteLastModified =
System.IO.File.GetLastWriteTime(sRemotePath)
'
' taking suggestion from KMARSH. now not checking to see if
the file is newer,
' copying the file if the modified dates don't match. this
will aid us when
' we switch environments...
'
If datLocalLastModified.Compare(datLocalLastModified,
datRemoteLastModified) <> 0 Then
'
' the remote file is newer, let's return true...
'
bReturn = True
Else
bReturn = False
End If
Else
bReturn = True
End If
IsModifiedFile = bReturn
End Function
Private Sub DisplayVersion()
Try
System.Windows.Forms.Application.DoEvents()
Dim sVersion As String
sVersion =
System.Reflection.Assembly.GetExecutingAssembly.GetName.Vers ion.ToString()
Dim aVersions() As String
aVersions = sVersion.Split(".")
frmCopyStatus.ucfstatus.Text = "Version " + aVersions(0) +
"." + aVersions(1) + "." + aVersions(2)
Catch ex As Exception
End Try
End Sub
Public Sub GetLatestCTSBuild()
Try
'Loop through the directories
For Each dir As System.IO.DirectoryInfo In dirs
If dir.LastWriteTime > dtLatest Then
dtLatest = dir.LastWriteTime
sLatestName = dir.Name
End If
Next
Catch ex As System.Exception
Throw ex
End Try
End Sub
End Class
End Namespace
File properties for the shared file:Attributes(archive is checked)
Secuity for the shared file: Administrators full control
File properties for the local file:Attributes(nothing is checked)
Secuity for the local file: Administrators full control; Users have read;
read & execute checked.