我想創建一個文件與ASP提示用戶給一個文件名,然後它將運行Sub CreateFile_Click這將創建新的文件。新文件應該與提示中輸入的用戶名稱相同。如何用asp.net創建一個新文件?
由於某種原因,即時得到
+System.IO.File.Create(filepath).Dispose()
{"Access to the path 'C:\futu.txt' is denied."}
System.UnauthorizedAccessException
<script language="VB" runat="server">
Sub CreateFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim strFileName As String = funcParam.Value
Dim filepath as String = "C:\futu.txt"
If Not System.IO.File.Exists(filepath) Then
System.IO.File.Create(filepath).Dispose()
End If
'custom code to create a text file
End Sub
</script>
<html>
<head>
<script language="JavaScript">
//ask for user input and then create file
function CreateFile() {
//get filename from the user
var fileName = prompt('Type the name of the file you want to create:', '');
//if the user clicks on OK and if they have entered something
if ((fileName) && (fileName != "")) {
//save the filename to the hidden form field 'funcParam'
document.forms['myForm'].elements['funcParam'].value = fileName;
//call the postback function with the right ID
__doPostBack('CreateFile', '');
}
}
</script>
</head>
<body>
<form runat="server" id="myForm">
<a href="javascript:CreateFile();">Create Text file</a>
<asp:linkbutton id="CreateFile" runat="server" onclick="CreateFile_Click" />
<input type="hidden" runat="server" id="funcParam">
</form>
</body>
</html>
您只能在Windows窗體應用程序,ASP.NET應用程序中訪問'c:',您可以訪問它,這是當前應用程序根文件夾中的文件夾,例如'C:\ inetpup \ mysite' – balexandre