Link to home
Start Free TrialLog in
Avatar of Victor  Charles
Victor CharlesFlag for United States of America

asked on

Help with creating folder on user's PC

Hi,

I am using clickonce technolody to deploy my project, would like to create a folder on user's PC to save a file, does the user need admin rights to create a folder on the C drive? If not, How do you create a folder (i.e. AP40) on the C drive using VB.NET?

Thanks,

Victor
Avatar of it_saige
it_saige
Flag of United States of America image

Creating a folder is easy (quick and dirty)
Imports System.IO

Module Module1
	Sub Main()
		CreateFolder("C:\Test")
		Console.ReadLine()
	End Sub

	Public Sub CreateFolder(ByVal folder As String)
		Dim dir As DirectoryInfo = New DirectoryInfo(folder)
		Try
			If Not dir.Exists Then
				dir.Create()
				Console.WriteLine("Directory {0}; created.", dir.FullName)
			Else
				Console.WriteLine("Directory {0}; already exists.", dir.FullName)
			End If
		Catch ex As Exception
			dir = New DirectoryInfo(folder)
			If Not dir.Exists Then
				Console.WriteLine("There was an error creating the directory - {0}.", dir.FullName)
			End If
			Console.WriteLine("{0}.", ex.Message)
		Finally
			If dir IsNot Nothing Then
				dir = Nothing
			End If
		End Try
	End Sub
End Module

Open in new window

Produces the following output -User generated imageBut, you asked a key question.
Does the user need admin rights?
It depends on the operating system (Vista and above, YES).  In order to provide admin rights on the creation of the folder, you could use an API call to ShellExecute.  You could also instruct the user to right-click on the installer and run as administrator.  Or, you could use a manifest file to instruct the application that it needs administrative rights.

But this raises another couple of questions.

Is the folder user or application specific?
Is there a dependency that requires the folder to be on the root of C:?

If the folder is specific to your application and is only needed for the installation, it is better to use a temporary file location that already exists and can be accessed by way of the environment variables.  If it is permanent and required, then if it is user specific, put it in the Users Application Data folder (again accessible through environment variables).  Otherwise, store it in the application folder.

-saige-
Something like this illustrates what I am referencing:
Imports System.IO

Module Module1
	Sub Main()
		CreateFolder("C:\Test")
		' Creating a user specific folder
		CreateFolder(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "EE_Q28615221\AP40"))
		' Creating an application specific folder
		CreateFolder(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "EE_Q28615221\AP40"))
		Console.ReadLine()
	End Sub

	Public Sub CreateFolder(ByVal folder As String)
		Dim dir As DirectoryInfo = New DirectoryInfo(folder)
		Try
			If Not dir.Exists Then
				dir.Create()
				Console.WriteLine("Directory {0}; created.", dir.FullName)
			Else
				Console.WriteLine("Directory {0}; already exists.", dir.FullName)
			End If
		Catch ex As Exception
			dir = New DirectoryInfo(folder)
			If Not dir.Exists Then
				Console.WriteLine("There was an error creating the directory - {0}.", dir.FullName)
			End If
			Console.WriteLine("{0}.", ex.Message)
		Finally
			If dir IsNot Nothing Then
				dir = Nothing
			End If
		End Try
	End Sub
End Module

Open in new window

Produces the following output -User generated image-saige-
Avatar of Victor  Charles

ASKER

Hi,

Thank you for both inputs, due to admin rights issues with Vista or higher, better approach will be to allow users to select the folder they want to save the file.

Thanks,

Victor
Hi,

Do you know if all windows pc have a c:\Temp folder, might be easier  to install to the temp folder.
ASKER CERTIFIED SOLUTION
Avatar of it_saige
it_saige
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank You.