add/change name/delete server files via vb.net web form

I have the add(upload) part of this page figured out.

I have an application with an "Images" sub directory.
I know how to access the getfiles() method from the system.IO namespace. I am using a while loop to create a table with an edit button (so that you can change the name of the file) and a delete button so you can remove the file from the server. Each file is on a new line much like it would look if it was bound to a datalist or datagrid.
Here is where I am clueless. Can anyone point me in the right direction? I am not looking for someone to write this for me, because after 3 years or so I am a little past that :)
I am just looking for a discussion or a tutorial on the methods I would need to implement here.
Thanks,
Eric
Using all I have learned from this site to make something I think is cool
0
aphotoguy
4/13/2004 7:43:06 PM
asp.net.web-forms 93655 articles. 6 followers. Follow

4 Replies
2045 Views

Similar Articles

[PageSpeed] 53

Check out the remarks on this page.  It shows you how to do various file and directory-related tasks.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemIOFileClassDeleteTopic.asp

Darrell Norton, MVP
Darrell Norton's Blog


Please mark this post as answered if it helped you!
0
DarrellNorton
4/13/2004 10:00:34 PM
Darrell,

Thanks for the link I should have thought to look in the msdn library, but sometimes your get hooked on these forums and begin to rely on them for suggestions.
With that said. I have it all figured out now. I have a sub directory called image the page uses system.io to look in that directory build an array with the filenames, then in my old asp.net unleashed book I found a section on arraylists and using a class to add multiple strings to an array list at once and binding that arraylist to a datalist. In the datalist I have delete button, edit where they can only change the base name of the file and not the extension. The file name in the list is also a hyperlink that opens a new window so the user can view the file that they are deleting or changing the name of.
The one last thing that I want to do is make it so that the user can not type into the textbox that is rendered by the <input type=file> tag. Any suggestions there?
Thanks so much.
Eric
I am posting my code for anyone that has been wondering how to do something like this.

'This is class that I use when populating the colArrayList
Public Class Files
Public CompleteFileName As String
Public FileName As String
Public Extension As String

Public Sub New( CompleteFileName As String, FileName As String, Extension As String )
MyBase.New()
Me.CompleteFileName = CompleteFileName
Me.FileName = FileName
Me.Extension = Extension
End Sub
End Class
'My page wide variable I use for a path of a file in different subroutines and functions
Dim PathVar As String
Sub Page_Load(Sender As Object, E As EventArgs)
If Not IsPostBack
Bindpage()
End If

End Sub
Sub Bindpage()

Dim AllFilesARR As Array
Dim colArrayList As ArrayList = new ArrayList()
AllFilesARR = BuildAllFilesArray()

Dim i As Integer
For i = 0 To AllFilesARR.length - 1
colArrayList.Add( New Files( AddComplete(AllFilesARR(i)), AddFileName(AllFilesARR(i)), AddExtension(AllFilesARR(i)) ) )
Next
FileList.DataSource = colArrayList
FileList.DataBind()
End Sub

Public Function BuildAllFilesArray() As Array
PathVar = ConfigurationSettings.AppSettings("AppPath") & "image"
Dim AllFilesARR As Array
Dim files As String() = Directory.GetFiles(PathVar)

AllFilesARR = Array.CreateInstance(GetType(String), files.length)
files.copyTo(AllFilesARR,0)

return AllFilesARR

End Function
Public Function AddComplete(s As String) As String
AddComplete = Path.GetFileName(s)
Return AddComplete

End Function
Public Function AddFileName(s As String) As String
AddFileName = Path.GetFileNameWithoutExtension(s)
Return AddFileName

End Function
Public Function AddExtension(s As String) As String
AddExtension = Path.GetExtension(s)
Return AddExtension

End Function
Sub Edit_Command(Sender As Object, E As DataListCommandEventArgs)

message.text= ""
FileList.EditItemIndex = e.Item.ItemIndex
Bindpage()
End Sub
Sub Cancel_Command(Sender As Object, E As DataListCommandEventArgs)
message.text= ""
FileList.EditItemIndex = -1
Bindpage()
End Sub

Sub Delete_Command(Sender As Object, E As DataListCommandEventArgs)
Dim fullname As Label
fullname = e.Item.FindControl("fullname")
PathVar = ConfigurationSettings.AppSettings("AppPath") & "image\" & fullname.text
File.Delete(PathVar)
message.text= "The file " & fullname.text & "has been successfully deleted."
FileList.EditItemIndex = -1
Bindpage()
End Sub
Sub Update_Command(Sender As Object, E As DataListCommandEventArgs)

Dim fullnamelbl As Label
fullnamelbl = e.Item.FindControl("fullnamelbl")
Dim filenametxt As Textbox
filenametxt = e.Item.FindControl("filenametxt")
Dim extensionlbl As Label
extensionlbl = e.Item.FindControl("extensionlbl")

PathVar = ConfigurationSettings.AppSettings("AppPath") & "image\" & fullnamelbl.text
Dim NewPath As String
NewPath = ConfigurationSettings.AppSettings("AppPath") & "image\" & filenametxt.text & extensionlbl.text
If File.Exists(NewPath) Then

message.text= "The new file name " & filenametxt.text & extensionlbl.text & " already exists.<br> Please delete " & filenametxt.text & extensionlbl.text & " FIRST before changing " & fullnamelbl.text & " to " & filenametxt.text & extensionlbl.text & "."

Else
' Move the file.
File.Move(PathVar, NewPath)
'delete original if it still exists
If File.Exists(PathVar) Then
File.Delete(PathVar)
End If
End If
FileList.EditItemIndex = -1
Bindpage()
End Sub
Sub Upload_Click(Sender As Object, E As EventArgs)
Dim filename as string = FileUpload.PostedFile.FileName()
Dim extension As String = Path.GetExtension(filename).Tolower()
If extension = ".jpg" OR extension = ".gif" OR extension = ".swf" Then
If FileUpload.PostedFile.ContentLength > 0
If not FileUpload.PostedFile.FileName.ToString.Equals("") Then
filename = filename.Tolower()
PathVar = ConfigurationSettings.AppSettings("AppPath") & "image\"
Dim c as string = System.IO.Path.GetFileName(filename)
FileUpload.PostedFile.SaveAs(PathVar + c)
uploadmessage.text= "File Uploaded"
Else
uploadmessage.text= "Upload failed"
End If
Else
uploadmessage.text= "Upload failed--The file contained no data."
End If
Else
uploadmessage.text= "Upload failed--The file is not a JPEG, GIF or SWF file."
End If
Bindpage()
End Sub
</script>
'Here is the page layout
<body runat="server">
<form EncType="multipart/form-data" runat="server">
<input id="fileupload" Type="file" Accept="image/gif, image/jpeg, application/x-shockwave-flash" Runat="server"/><asp:label id="uploadmessage" text="" Runat="server"/><br><br>
<asp:Button id="upload" Text="Upload File" OnClick="Upload_Click" Runat="server"/><br>
<br>
<asp:label id="message" text="" Runat="server"/>
<table width="100%" >
<tr>
<td valign="top" align="center">
<asp:DataList id="FileList"
OnDeleteCommand="Delete_Command"
OnUpdateCommand="Update_Command"
OnEditCommand="Edit_Command"
OnCancelCommand="Cancel_Command"
RepeatColumns="1"
RepeatDirection="Horizontal"
GridLines="None"
Runat="server">
<HeaderTemplate>
<table>
<tr>
<td>
</td>
<td align="center">
File Name
</td>
<td>
</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td align="left">
<asp:button id="delete" text="Delete File" width="150" CommandName="Delete" Runat="server"/>
</td>
<td align="center" width="300">
<asp:label id="fullname" text='<%# Container.DataItem.CompleteFileName %>' Runat="server"/>
</td>
<td align="right">
<asp:button id="edit" text="Change File Name" width="150" CommandName="Edit" Runat="server"/>
</td>
</tr>
</ItemTemplate>
<EditItemTemplate>
<tr>
<td align="left">
<asp:button id="cancel" text="Cancel Changes" width="150" CommandName="Cancel" Runat="server"/>
</td>
<td align="center"><asp:label id="fullnamelbl" text='<%# Container.DataItem.CompleteFileName %>' Visible="false" Runat="server"/>
<asp:textbox id="filenametxt" width="275" text='<%# Container.DataItem.Filename %>' Runat="server"/><asp:label id="extensionlbl" text='<%# Container.DataItem.Extension %>' Runat="server"/>
</td>
<td align="right">
<asp:button id="update" text="Save Changes" width="150" CommandName="Update" Runat="server"/>
</td>
</tr>
</EditItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:DataList><br>
The file name is clickable and will open a new window in order to view it.
</td>
</tr>
</table>

I know this is long, but I think that it is very usefull for people that haven't thought about doing something like this.
Using all I have learned from this site to make something I think is cool
0
aphotoguy
4/15/2004 3:39:27 PM
Thanks for posting the code.  I'm sure it will help at least 1 other developer with a similar question!
Darrell Norton, MVP
Darrell Norton's Blog


Please mark this post as answered if it helped you!
0
DarrellNorton
4/15/2004 4:11:06 PM
no problem.  It works really well.  It even kind of suprises me. :)  Do you love when that happens.

To all of those out there that use this code, just drop me a note in here or through my profile and say hi. That's all.
Eric
Using all I have learned from this site to make something I think is cool
0
aphotoguy
4/15/2004 4:47:24 PM
Reply:

Similar Artilces:

windows forms in vb .net into web forms
we are creating a database driven website as part of our project. currently we have forms connecting to the database created in visual basic. these windows forms we need to convert into web forms which are asp .net compatible. can u give me an answer or show me a way. Hi, because of the difference in nature between web and win applications you'll need to reimplement at least the UI part. If you have a multitiered application in which the busines logic and data access are in different parts/assemblies you can reuse them. Grz, Kris.Read my blog. Handy Firefox plugins for web developers.Wor...

Implementing .Net Form into an APS.net web form, can this be done
I am new here, but have been searching for a while, and may not have the correct lingo to find what I am looking for.  I am tasked with implementing a .exe application that was writen vb6 then converted to .net into a new website my team is developing. The idea is to put each of the 3 different forms in this .exe application of 3 different .aspx pages. I have attempted multiple things to get this into the page and even started to just rewrite it as an ASP.net web form, but even then I can't reuse any of the code since the System.Web.UI.Page doesn't inherit the sa...

Deploying Vb.Net Web Forms on the server
I created a setup file for my first test Web Application. It has a few forms that I like to test on the server. What do I have to do on the server for it to work properly. I have it working on my dev machine. What do I have to install and is there any changes that I have to make to machine.config or any other files. I am using the default settings in Global and web.config files. These files were generated by the Vb.Net 2003. I have been reading on this for awhile and like to make sure of the steps I have to follow. Any help will be greatly appreciated....

how to open .exe file in a web form in vb.net?
hi, Are there any one who know to open .exe file from a web form application? make a link like: My Exe ...

change web form background property from code in vb.net
I have a web form in a vb.net project. The web form has both a design view and an HTML view. In the design view, the property box has a background property that can be set to an image file. This causes a background attribute to be added to the Body element in the HTML view. My question is, how do I modify the background property from program code? I can't find the object that has this property so I can set its value. On way to do it is to use 2 different style sheet and change the stylesheet in the pageload event depending on your condition. Look for an example in Report Starter Kit ...

Visual Studio .NET cannot create or open the application. The likeliest problem is that required components are not installed on the local Web server. Run Visual Studio .NET setup and add the Web Deve
I installed Visual Studio .NET 2003 on Windows Vista Home Premuim, but I'm unable to select "Web Development" item from "Server Components" during the installation. I installed all the pre-requisits but I can not see Front Page Server Extention in the programs. I can see also the IIS. Appreciate if any one can guide me with the solution.   Thanks.   FPSE is not included with Vista, it's a separate download.  See www.iis.net for this. JeffPlease: Don't forget to click "Mark as Answer" on the post that helped you. Tha...

How can I coding --> auto refreshing Crystal report via vb.net-->web form
please help me I must show the report(real time) that data come from database --> (data are from another program) User open my webpage for looking the report-->update data How can I set report automatic. --> automatic refresh<----- thanks so much If your report is using the CrystalReportViewer, and you are not exporting the report to PDF or Excel, I would simply set a META Refresh in the header. Here is a reference: http://www.htmlhelp.com/reference/html40/head/meta.html This will cause the page to reload from the server at a specified interval....

WEB FORMS and datasetS (do I have to repeat the file definition in every web form?)
I am working on vb.net.I have a project with 35 web forms and an access file with 18 tables.Until now I have defined the oledbadapters in every form by dragging the tables from the server explorer tool box.This is not a good idea because every time that I make a change on the database design I have to drag the new definition on 35 web forms and I have to create the datasets again. This is boring and time consuming!The question is:WHAT CAN I DO TO HAVE THE FILE DEFINITION ONLY IN ONE PLACE AND THAT EVERY WEB FORM WOULD TAKE THE DATA ADAPTERS AND DATASETS FROM THERE? how about if you write a...

.Net Web Form & Web Service Deployment
Hi all I have written some test web services and some web form applications and deployed them to my IIS. That's fine but I now want to deploy them to my real IIS server. I have created an msi for each but the problem is I seem to only be able to deploy to the default web site. Our default web site is turned off and instead we use several other sites. Does anybody happen to if Powerbuilder 11 supports non default web sites? If not do I need to hack the msi and if so can anybody suggest a suitable tool. I have Orca but it's not very friendly. Unless of course I knew exactly w...

Web forms and mobile web forms
Hey everyone, I am looking to develope a web site that depending on the device connecting to it, it will display a certain page. In this case im looking to create a web and wap version of the same project. I have the Web forms version finished using ASP.NET and C# and I am wondering how to create the mobile web form and incorporate it in the project. In the project folder, can I just add a mobile web??? But what about displaying device specific pages?? Cheers -Dudach Yes, you can add mobile web forms and controls to your Web Application project. No problem. For device/br...

How to Make Web Forms from other Web Forms
In putting together an ASP.NET application for a biz application, we have come to the point where there are three different types of basic forms which have, for the most part, been perfected. Now its is a matter of cloning those forms for various uses and modifying or adding code to suit the particular need. Copy and paste works OK, but there is a lot of changing of certain words, etc. Is there a way in ASP.NET to take these basic forms, turn them into classes or templates, create forms from them, then add or overwrite code? ? (Anybody who is a Vis Fox Pro programmer would know imme...

how to read a .txt file from .net (web form)
hi, how to read a .txt file line by line and place the contents in a .xls file. ex: if sample.txt file is... Name: abc Company: xyz info ltd. i want abc and xyz info ltd to be cut and saved in .xls file. i already have a .xls template with name and company fields. send solution.......

Looping through controls in VB.Net Web Form
I need to disable every imagebutton. In VB6, I would have done it like this: 1 Dim iButton As ImageButton 2 For Each iButton In Me.Controls 3 iButton.Enabled = False 4 Next In .Net, I get a "Specified cast is not valid" errror on line 2. Using Page.controls in place of Me.Controls also produces the same error. So, I tried this (and multiple variations): 1 Dim ibControl As Object 2 For Each ibControl In Me.Controls 3 If TypeOf ibControl Is ImageButton Then 4 ...

login form for web services vb.net
Hi, I'm trying to create a windows form with  web services that check if the user and password are the same within the SQL SERVER DATABASE, if its the same then your a valid user if not your an invalid user. That is working ok, but I want to implement a try statement which only allows the user to enter in the details 3 times then the windows form shuts down. also if they do have it correct then another window opens up. ANY HELP PLEASE OR IF SOME 1 KNOWS HOW TO DO IT IN  ASP.NET :>  HERE THE CODE: <WebMethod()> _     &nbs...

Web resources about - add/change name/delete server files via vb.net web form - asp.net.web-forms

Resources last updated: 11/24/2015 7:20:37 AM