所以我正在尝试编译一个Asteroids游戏.它几乎正常工作,所有文件都到位等等......
当它遇到这个代码时会出现问题.
FileStream myFileStream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
string myTempFile = @"F:\Documents\Junior School\Computer Programming (Java 1)\AsteroidsWithSound\AsteroidsWithSound\temp\mysound" + i.ToString() + ".wav";
Run Code Online (Sandbox Code Playgroud)
它给了我一个错误/警告,不知道究竟它叫什么,但它说
ArgumentException未处理.空路径名称不合法.
我在网上看过像这样的代码块引起了这个问题但却找不到解决方案.任何帮助都是极好的.
编辑:文件名在此块中定义.
string filename = this.Player.FileName;
this.Player.Open("");
File.Delete(filename);
this.isReady = true;
Run Code Online (Sandbox Code Playgroud) 我的一位客户在保存文件时遇到了WPF应用程序崩溃的问题.
我的保存文件代码是:
var saveFileDialog = new SaveFileDialog {
InitialDirectory = string.Concat(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), @"\MyApp"),
FileName = "MyFile",
OverwritePrompt = true,
AddExtension = true
};
if (saveFileDialog.ShowDialog() == true) {
...
}
Run Code Online (Sandbox Code Playgroud)
以下是他们获得的例外情况:
Value does not fall within the expected range.
A System.ArgumentException occurred
at MS.Internal.Interop.HRESULT.ThrowIfFailed(String message)
at MS.Internal.AppModel.ShellUtil.GetShellItemForPath(String path)
at Microsoft.Win32.FileDialog.PrepareVistaDialog(IFileDialog dialog)
at Microsoft.Win32.FileDialog.RunVistaDialog(IntPtr hwndOwner)
at Microsoft.Win32.FileDialog.RunDialog(IntPtr hwndOwner)
at Microsoft.Win32.CommonDialog.ShowDialog()
Run Code Online (Sandbox Code Playgroud)
(ShowDialog最后一行中的位置是指我在上面的代码中进行的调用.)
所以我的预感是,在我的客户的情况下,对Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)的调用返回了一些SaveFileDialog不喜欢的内容InitialDirectory.我在Web搜索中找到(并验证)在传递InitialDirectorySaveFileDialog 的相对路径时发生此错误.是否有可能Environment.SpecialFolder.MyDocuments作为相对路径返回?如果没有,是否有人知道另一种可能无效的格式?某个SpecialFolder.MyDocuments网络路径可能是原因吗?还有其他想法吗?
我没有直接访问我客户的机器,他们不是特别精通技术,所以不可能100%确定发生了什么.
我在C#中的部分代码中抛出ArgumentNullException。我想捕获并显示我的异常的错误消息。但是没有参数名称。我想将参数名称传递给异常构造函数。
throw new ArgumentNullException("myParameter", errorMessageStringVariable);
Run Code Online (Sandbox Code Playgroud)
如果我打电话给error.Message我
errorMessageStringVariable参数名称:myParameter
我只想显示errorMessageStringVariable。是否可以使用ArgumentNullException,而无需对error.Message进行某种格式化?
c# exception-handling exception argumentexception argumentnullexception
背景信息:我正在使用服务中实现的FileSystemWatcher类来监视文件中的更改.下面是触发onCreate事件时抛出Argument Exception(Path不是合法形式)的编码部分.
FileMonitor.CS
public partial class FileMonitor:ServiceBase
{
public FileSystemWatcher Watcher = new FileSystemWatcher();
Private void FileWatcher()
{
FileActionHandler ActionHandler = new FileActionHandler();
Watcher.Created += new FileSystemEventHandler(ActionHandler.onCreate);
Watcher.Deleted += new FileSystemEventHandler(ActionHandler.onDelete);
Watcher.Renamed += new RenamedEventHandler(ActionHandler.onRenamed);
Watcher.EnableRaisingEvents = true;
}
}
Run Code Online (Sandbox Code Playgroud)
FileActionHandler.CS
class FileActionHandler
{
FileMonitor FileMon = new FileMonitor();
public void onCreate/onRename/onDelete(object source, FileSystemEventArgs e)
{
try
{
FileMon.Watcher.EnableRaisingEvents = false;
}
catch
{
/* Exception Code */
}
finally
{
FileMon.Watcher.EnableRaisingEvents = true;
}
}
}
Run Code Online (Sandbox Code Playgroud)
问题:任何人都可以告诉我为什么抛出异常以及如何解决它?
我使用dotnet 3.5和ComponentFactory Krypton v4.4.0.0支持Winforms应用程序,最近我实现了AppDomain.CurrentDomain.UnhandledException和Application.ThreadException处理程序,以通知我客户端上发生的异常,并发现了很多错误在日志中。此刻正在我的脑海:
System.ArgumentException: Parameter is not valid.
at System.Drawing.Font.GetHeight(Graphics graphics)
at System.Drawing.Font.GetHeight()
at System.Drawing.Font.get_Height()
at System.Windows.Forms.Control.set_Font(Font value)
at System.Windows.Forms.DataGridViewComboBoxEditingControl.ApplyCellStyleToEditingControl(DataGridViewCellStyledataGridViewCellStyle)
at System.Windows.Forms.DataGridView.BeginEditInternal(Boolean selectAll)
at System.Windows.Forms.DataGridView.ProcessKeyEventArgs(Message& m)
at System.Windows.Forms.Control.ProcessKeyMessage(Message& m)
at System.Windows.Forms.Control.WmKeyChar(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.DataGridView.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam,IntPtr lparam)
Run Code Online (Sandbox Code Playgroud)
请注意,stacktrace完全在Windows代码中。我的一门课还有另外一门课:
System.ArgumentException: Parameter is not valid.
at System.Drawing.Font.GetHeight(Graphics graphics)
at System.Drawing.Font.GetHeight()
at System.Drawing.Font.get_Height()
at System.Windows.Forms.Control.set_Font(Font value)
at MyOrg.MyApp.WindowsClient.GuiControls.MaskedTextBoxEditingControl.ApplyCellStyleToEditingControl(DataGridViewCellStyledataGridViewCellStyle)
at System.Windows.Forms.DataGridView.BeginEditInternal(Boolean selectAll)
at System.Windows.Forms.DataGridView.ProcessKeyEventArgs(Message& m)
at System.Windows.Forms.Control.ProcessKeyMessage(Message& m) …Run Code Online (Sandbox Code Playgroud) 在我们教授的示例代码中,他有一个看起来像这样的片段:
if (name == null || name == "")
throw new ArgumentException("name is null or empty");
Run Code Online (Sandbox Code Playgroud)
另一个看起来像这样的片段:
if (!File.Exists(name))
{
throw new Exception("File does not exist!");
}
Run Code Online (Sandbox Code Playgroud)
我只是想知道不同之处是什么以及为什么一个在另一个之上使用
当我尝试将进程'信息写入控制台时,我得到System.ArgumentException和System.ComponentModel.Win32Exception.是什么导致这个?我怎么能停止那些?
Process processListe = Process.GetProcesses();
for (int i = 0; i < processListe.Count(); i++)
{
try
{
string companyName = processListe[i].MainModule.FileVersionInfo.CompanyName;
string fileVersion = processListe[i].MainModule.FileVersionInfo.FileVersion;
Console.WriteLine(companyName + " " + fileVersion);
}
catch (Exception) { }
}
Run Code Online (Sandbox Code Playgroud)
错误发生在"string companyName = processListe [i] .MainModule.FileVersionInfo.CompanyName;"中 线.
错误消息:
System.ArgumentException: Illegal characters in path.
at System.Security.Permissions.FileIOPermission.HasIllegalCharacters(String[] str)
at System.Security.Permissions.FileIOPermission.AddPathList(FileIOPermissionAccess access, AccessControlActions control, String[] pathListOrig, Boolean checkForDuplicates, Boolean needFullPath, Boolean copyPathList)
at System.Security.Permissions.FileIOPermission..ctor(FileIOPermissionAccess access, String[] pathList, Boolean checkForDuplicates, Boolean needFullPath)
at System.IO.Path.GetFullPath(String path)
at System.Diagnostics.FileVersionInfo.GetFullPathWithAssert(String …Run Code Online (Sandbox Code Playgroud) 我有以下代码给我提出问题,我将不胜感激任何帮助:
private static string CreateOptionString(List<VehicleOption> Options)
{
StringBuilder returnValue = new StringBuilder();
foreach (VehicleOption option in Options)
{
if (option.OptionStatus == ExtendedResponse.OptionState.Included)
{
if (returnValue.Length > 0)
{
returnValue.Append(", ");
}
returnValue.Append(option.OptionName);
}
}
return returnValue.ToString();
}
Run Code Online (Sandbox Code Playgroud)
我最初的问题是我得到了一个System.InvalidOperationException:我的foreach循环上的集合被修改了错误.
1)我仍然无法弄清楚为什么我会得到这个错误,因为我没有看到它被修改的任何地方.
有人建议我将List复制到一个新List并循环遍历新List.我做到了,它摆脱了InvalidOperationException.但是,我试着以不同的方式处理列表,并且两者都给了我一个System.ArgumentException:目标数组不够长.以下是我尝试复制列表的两种方法
List<VehicleOption> newOptions = new List<VehicleOption>(Options);
Run Code Online (Sandbox Code Playgroud)
和
List<VehicleOption> newOptions = new List<VehicleOption>();
newOptions.AddRange(Options);
Run Code Online (Sandbox Code Playgroud)
这两个都给了我一个System.ArgumentException:Destination数组不够长.
2)为什么这些方法中的任何一个都会给我这个例外?
任何帮助将不胜感激,因为我很难过.
谢谢!
我有点困惑.
MSDN对ArgumentException的说法是:
"当提供给方法的其中一个参数无效时抛出的异常."
因此,如果传递给我的方法的参数无效,我会抛出异常.
但我也发现建议返回boolean加一个字符串. - 而不是抛出异常.
所以,
问题:当验证方法参数时,以及当参数无效时,验证方法应该何时抛出异常而何时不应该抛出异常?
所以我目前正在开发一个工作/工作得非常好的图像缩放器,但是当我一次处理太多图像时崩溃程序时它给了我一个OutOfMemoryException.
所以为了解决这个问题,我已经将方法包装在using语句中,这样可以正确处理Bitmaps.
但是我注意到如果我在using语句中返回我的Bitmap,我得到这个"ArgumentException未处理"消息
这是我的ImageResize方法:
public Bitmap ResizeImage(MemoryStream ms, Size size)
{
if (comboBox2.Text == "Pixel")
{
using (Bitmap img = new Bitmap(new Bitmap(ms, true), size.Width, size.Height))
{
var original = new Bitmap(ms, true);
Graphics graphic = Graphics.FromImage(img);
//IRRELEVANT CODE.....
return img;
}
}
else
{
return null;
}
Run Code Online (Sandbox Code Playgroud)
在这里,当我尝试将图像保存在ImageResize方法之外时:
private void button1_Click(object sender, EventArgs e)
{
//IRRELEVANT CODE ...
img = ResizeImage(memory, new Size(getX(), getY()));
//IRRELEVANT CODE ...
img.Save(outputFileName, codec, encoderParams); //<-Exception occurs here
}
Run Code Online (Sandbox Code Playgroud)
当我删除using语句时,一切都工作得很好,但是我必须使用using块来处理Bitmap,以防止内存泄漏.此外,当我将图像保存在using语句中时,它也可以正常工作,但在我的情况下,这不是解决方案.
我究竟做错了什么?对我来说,似乎没有正确返回Bitmap.
我感谢任何帮助,并提前感谢Ravand