VB.NET 2008、Windows 7 和保存文件

发布于 2024-09-02 02:41:22 字数 1080 浏览 3 评论 0原文

这学期我们必须学习 VB.NET,我的经验主要是 C#——但这并不会对这个特定问题产生影响。

我使用了 .NET 框架最简单的方法来保存文件,但 Windows 7 不允许我将文件保存在任何地方(或我已找到的任何地方)。这是我用来保存文本文件的代码。

Dim dialog As FolderBrowserDialog = New FolderBrowserDialog()
Dim saveLocation As String = dialog.SelectedPath
... Build up output string ...
Try
    ' Try to write the file.
    My.Computer.FileSystem.WriteAllText(saveLocation, output, False)
Catch PermissionEx As UnauthorizedAccessException
    ' We do not have permissions to save in this folder.
    MessageBox.Show("Do not have permissions to save file to the folder specified. Please try saving somewhere different.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Catch Ex As Exception
    ' Catch any exceptions that occured when trying to write the file.
    MessageBox.Show("Writing the file was not successful.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try

问题是,无论我尝试在哪里保存文件,使用此代码都会引发 UnauthorizedAccessException。我尝试以管理员身份运行 .exe 文件,并以管理员身份运行 IDE。

这只是 Windows 7 的过度保护吗?如果是这样,我可以做什么来解决这个问题?要求规定我能够保存文件!

谢谢。

We have to learn VB.NET for the semester, my experience lies mainly with C# - not that this should make a difference to this particular problem.

I've used just about the most simple way to save a file using the .NET framework, but Windows 7 won't let me save the file anywhere (or anywhere that I have found yet). Here is the code I am using to save a text file.

Dim dialog As FolderBrowserDialog = New FolderBrowserDialog()
Dim saveLocation As String = dialog.SelectedPath
... Build up output string ...
Try
    ' Try to write the file.
    My.Computer.FileSystem.WriteAllText(saveLocation, output, False)
Catch PermissionEx As UnauthorizedAccessException
    ' We do not have permissions to save in this folder.
    MessageBox.Show("Do not have permissions to save file to the folder specified. Please try saving somewhere different.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Catch Ex As Exception
    ' Catch any exceptions that occured when trying to write the file.
    MessageBox.Show("Writing the file was not successful.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try

The problem is that this using this code throws an UnauthorizedAccessException no matter where I try to save the file. I've tried running the .exe file as administrator, and the IDE as administrator.

Is this just Windows 7 being overprotective? And if so, what can I do to solve this problem? The requirements state that I be able to save a file!

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巴黎盛开的樱花 2024-09-09 02:41:22

此代码:

Dim dialog As FolderBrowserDialog = New FolderBrowserDialog()
Dim saveLocation As String = dialog.SelectedPath

为您提供文件夹的位置。然后您尝试使用与文件夹同名的文件来保存文件。相反,我假设您想在该文件夹内保存一个文件:

Dim saveLocation As String = dialog.SelectedPath
saveLocation = Path.Combine(saveLocation, "SomeFile.txt")

这将在所选文件夹内创建一个名为“SomeFile.txt”的文件。

或者,不要使用FolderBrowserDialog来选择文件夹,而是使用 SaveFileDialog 选择实际文件。

This code:

Dim dialog As FolderBrowserDialog = New FolderBrowserDialog()
Dim saveLocation As String = dialog.SelectedPath

Is giving you the location of a folder. Then you're trying to save a file with the same name as the folder. Instead, I assume you want to save a file inside that folder:

Dim saveLocation As String = dialog.SelectedPath
saveLocation = Path.Combine(saveLocation, "SomeFile.txt")

That will create a file called "SomeFile.txt" inside the selected folder.

Alternatively, instead of using FolderBrowserDialog to choose a folder, use SaveFileDialog to select the actual file instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文