?
Solved

Help with creating folder on user's PC

Posted on 2015-02-11
6
Medium Priority
?
87 Views
Last Modified: 2015-02-18
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
0
Comment
Question by:vcharles
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 34

Expert Comment

by:it_saige
ID: 40604496
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 -Capture.JPGBut, 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-
0
 
LVL 34

Expert Comment

by:it_saige
ID: 40604520
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 -Capture.JPG-saige-
0
 

Author Comment

by:vcharles
ID: 40604534
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
0
Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

 

Author Comment

by:vcharles
ID: 40604912
Hi,

Do you know if all windows pc have a c:\Temp folder, might be easier  to install to the temp folder.
0
 
LVL 34

Accepted Solution

by:
it_saige earned 2000 total points
ID: 40604949
All windows computers do have a TEMP folder, but they are not all on the root of C:.  Also, based on Software Group Policies or just general network/computer security, it is a common practice to limit certain *tasks* from being accomplished in the TEMP folder (think restricting the ability to run or start a program and/or script from the TEMP folder).

If this is not a concern and for completeness, to access the Temp folder (regardless of it's defined location), you would use the GetTempPath() method in the Path class (which uses the GetTempPath API function).
The GetTempPath function checks for the existence of environment variables in the following order and uses the first path found:

      1.The path specified by the TMP environment variable.
      2.The path specified by the TEMP environment variable.
      3.The path specified by the USERPROFILE environment variable.
      4.The Windows directory.

Note that the function does not verify that the path exists, nor does it test to see if the current process has any kind of access rights to the path. The GetTempPath function returns the properly formatted string that specifies the fully qualified path based on the environment variable search order as previously specified. The application should verify the existence of the path and adequate access rights to the path prior to any use for file I/O operations.
Here is the above program using GetTempPath.
Imports System.IO

Module Module1
	Sub Main()
		' Creating an application folder in the temp directory
		CreateFolder(Path.Combine(Path.GetTempPath(), "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

Which produces the following output -Capture.JPG-saige-
0
 

Author Closing Comment

by:vcharles
ID: 40617009
Thank You.
0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question