Opening a file from a web app VB.NET - vb.net

I am currently trying to open a file on my web app by retrieving the file path from a sql database
here is my code that will open the file when I double click an index in a listbox.
Protected Sub userdoubleClick()
Dim selectedPath As String
Try
fileConnection.Open()
selectedFile = FileListBox.SelectedValue
'takes the selected items from the listbox and searches the database for the file
'and will open the file for the user
fileCommand.CommandText = "SELECT filePath FROM ImportedFiles WHERE FileName = '" & selectedFile & "'"
fileDataReader = fileCommand.ExecuteReader
Do While fileDataReader.Read
selectedPath = fileDataReader.GetValue(0)
Loop
System.Diagnostics.Process.Start(selectedPath)
Catch ex As Exception
End Try
fileConnection.Close()
End Sub
When I run this on my local PC it works fine but then when I publish it to a server it wont open the file.

System.Diagnostics.Process.Start(selectedPath) will open the file on the machine running the web application; in other words, it is opening it on your local machine when running locally, but opening on the server when deployed. This just isn't going to work.
See ASP.Net Download file to client browser

If you're accessing the files from a web application, then the user identity under which the file is opened is not that of the user logged into your application. It's the user under which the IIS application pool is running. That's the user that needs to have permission granted to your shared drive.
There are many solutions to this problem, including changing the context under which the application pool is running or making the files local.

Related

Able to write file but not read from a network location

I have following network location
Dim myfolder As String = "\\10.0.0.90\myfolder\"
I am able to create a new file in this folder using following code:
File.Create (myfolder)
But when I try to read contents of this folder using code below I get error.
Code
Dim orderedFiles = New System.IO.DirectoryInfo(myfolder).GetFiles()
Error
The system detected a possible attempt to compromise security. Please
ensure that you can contact the server that authenticated you.
File writing is being done by ASP.Net page while reading is done from Windows Service. Could this be the issue?
Windows Service was running as "Local System". I right click on it, went into properties and changed the "Log on as" to some user account and now it can access network folder.

FolderBrowseDialog to browse folder of server computer from client computer

I want to open the directory of server computer using FolderBrowserDialog from the client machine.
Is it possible?
I have searched for it n haven't found the solution.
I have found others such as to browse Network Folders, here: How-to-Browse-Network-Folders-using-Folder-Dialog .
But i am looking for to browse the directory of server from the client.
here is a snippet that works fine with .net 4:
Dim dlg As New FolderBrowserDialog()
dlg.SelectedPath = "\\yourServer\share"
If dlg.ShowDialog() = DialogResult.OK Then
Dim selected = dlg.SelectedPath
' ...
End If
no need for RootFolder or any hacking - this will open the dialog on my computer (both client/server are windows-OS in a windows-domain) at the network-share and let my trill down into the folders there - at the end you will get a string like \\yourServer\share\selected\path back

DirectoryInfo.Exists Returns false with UNC path

Well, I have an application which is on a non-domain tablet.
On start the application shells commands the proper creds to a network unc path
Shell("net use \\mypath\destloca password username")
this command is correct and does what it is suppose to.
Next, I call
Dim dir As New IO.DirectoryInfo(loca)
If Not dir.Exists Then
MessageBox.Show("There are no records for loca", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification)
Exit Sub
End If
This usually works fine however every once in a while it will return false. The network is connected but i am not able to get to domain resources.
However, after I Shell again I am able to access the UNC path.
Anybody got any ideas?

A portion of the path on the network can not be found

I have built a service on a windows server 2008 R2 server that executes a windows forms application. That is all working but when I want to write an error to the log on a shared network location i get the error.
But the weird thing is that when I execute the executable from the windows forms project as administrator it's working, but when I run the service under a administrator account i get the error .
This is the code how I write to the log file
So i first try to write to L:\ and when that's not working I write in C:\ what is working
Try
Using writer As StreamWriter = New StreamWriter("L:\subfolder\subfolder\log.txt", True)
writer.WriteLine(Now & " - project started. Build 7-12-2012")
End Using
Catch ex As System.Exception
Using writer As StreamWriter = New StreamWriter("c:\subfolder\log.txt", True)
writer.WriteLine(Now & " - ERROR " & ex.ToString)
End Using
Finally
End Try
Mapped drives are often user and session dependent, so while you are running the program on the desktop, drive L exists, but when running as a service, its likely drive L was not mapped and doesn't exist. I suggest using the full network path, like \\servername\sharedfolder\...

FileUpload SaveAs UnauthorizedAccessException error (Dotnetnuke)

I am trying to save an image file in a custom module I am building for a DNN site.
However when I run the code I get an UnauthorizedAccessException.
if(upLoadAddImg.HasFile)
{
String imageLocation = ConfigurationManager.AppSettings["ImageFolderPath"];
//Upload file
upLoadAddImg.SaveAs(Server.MapPath(imageLocation));
}
I am running on localhost using the internal visual studio server. Tthe folderpath is all right and I have made sure Network Service has full permissions.
Am I missing something obvious or does DNN have some special permission setting I am missing?
If you are using the dev server in VS its identity is not Network Service by default. Most likely the directory you are saving to loccally is not allowed for the aspnet user - if you run this on a web site it should work at least code wise -------
To test it you can do one of two things make the portal directory open to everyone or set up a local site not run on the dev server ----