本文介绍了ASP.NET中“/”应用程序中的服务器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 运行我的网站时,我在'/'应用程序中遇到服务器错误。我已经尝试解决问题,但我仍然无法弄清楚错误的来源。它声明该进程无法访问该文件,因为它正由另一个进程使用。在此之前,我创建了一个代码,如果管理员想要创建一个新品牌,它将自动在我定位到的文件上编写新的代码片段。而且如果管理员想要删除该品牌,它将删除我之前写的所有新代码。 昨天一切正常但是视觉工作室正在促使我对IDE外部的文件进行了更改,并继续检查代码中的一致行。我只是单击是以全部更新代码。但我希望它自动重新加载文件,所以我去了工具 - 选项 - 文档 - 然后我检查重新加载修改的文件,除非有未保存的更改。在那之后,我仍然可以运行我的网站并创建新的品牌,但每当我尝试删除该品牌时,它会在'/'应用程序中给我这个服务器错误。 以下是错误的完整描述: '/'应用程序中的服务器错误。 该进程无法访问文件'C:\Users \User1 \Documents\Visual Studio 2015 \WebSites\MusicStore \ App_Code \ ConnectionClassGuitarItems.cs',因为它正由另一个进程使用。 描述:执行当前Web请求期间发生了未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。 异常详细信息:System.IO.IOException:进程无法访问文件'C:\ Users \User1 \Documents\Visual Studio 2015 \ WebSites\MusicStore \ App_Code \ ConnectionClassGuitarItems.cs'因为它被另一个进程使用。 来源错误: 第184行:} 第185行: 第186行:System.IO.File.WriteAllText(@ C:\ Users \User1 \Documents\Visual Studio 2015 \WebSites\MusicStore\App_Code\ConnectionClassGuitarItems.cs,结果); 第187行:} 第188行:} 源文件:c:\ Users \User1 \Documents\Visual Studio 2015 \WebSites \ MusicStore \Pages \OverviewGuitarData.aspx.cs行:186 Stack Trace: [IOException:进程无法访问文件'C:\Users \User1 \Documents\Visual Studio 2015 \WebSites\MusicStore\App_Code\ConnectionClassGuitarItems.cs'因为它正由另一个进程使用。] System.IO .__ Error.WinIOError(Int32 errorCode,String maybeFullPath)+216 System.IO.FileStream.Init(String path,FileMode mode,FileAccess access,Int32 rights,Boolean useRights,FileShare share ,Int32 bufferSize,FileOptions选项,SECURITY_ATTRIBUTES secAttrs,String msgPath,Boolean bFromProxy,Boolean useLongPath,Boolean checkHost)+1326 System.IO.FileStream..ctor(String path,FileMode mode,FileAccess access, FileShare共享,Int32 bufferSize,FileOptions选项,String msgPath,Boolean bFromProxy,Boolean useLongPath,Boolean checkHost)+66 System.IO.StreamWriter.CreateFile(String path,Boolean append,Boolean checkHost)+73 System.IO.StreamWriter..ctor(String path,Boolean append,Enco ding encoding,Int32 bufferSize,Boolean checkHost)+73 System.IO.File.InternalWriteAllText(String path,String contents,Encoding encoding,Boolean checkHost)+64 系统.IO.File.WriteAllText(String path,String contents)+43 Pages_OverviewData.RemoveConnectionClassGuitarItems(String name)in c:\ Users \User1 \Documents\Visual Studio 2015\WebSites \ MususStore \Pages \OverviewGuitarData.aspx.cs:186 Pages_OverviewData.GuitarBrandsGridViewBtn_Click(Object sender,EventArgs e)位于c:\ Users \ User1 \Documents\Visual Studio 2015 \ WebSites\MusicStore \Pages \OverviewGuitarData.aspx.cs:73 System.Web.UI.WebControls.Button.OnClick(EventArgs e)+9696694 System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)+204 System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)+12 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl,String eventArgument)+15 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData )+35 System.Web.UI.Page.ProcessRequestMain(布尔includeStagesBeforeAsyncPoint,布尔includeStagesAfterAsyncPoint)+1639 版本信息:Microsoft。 NET Framework版本:4.0.30319; ASP.NET版本:4.6.1055.0 我尝试过:I've been having a Server Error in '/' Application when im running my website. I have already tried troubleshooting the problem but i still can't figure out where the error is coming from. It states that the process cannot access the file because it is being used by another process. Before this happens, I created a code where if the admin wants to create a new brand, it will automatically write new piece of code on files that i target it to. And also if the admin wants to delete the brand, it will remove all the new code that i wrote previously.Everything worked fine yesterday but visual studio is prompting me that there are changes made on files outside of IDE and it kept on checking for consistent lines on my code. I just clicked yes to all to update the code. But i want it to automatically reload the files so i went to Tools-Options-Documents-then i checked Reload modified files unless there are unsaved changes. After that, i can still run my website and create new brands but whenever i try to remove the brand, it is giving me this server error in '/' application.Here is the full description of the error:Server Error in '/' Application.The process cannot access the file 'C:\Users\User1\Documents\Visual Studio 2015\WebSites\MusicStore\App_Code\ConnectionClassGuitarItems.cs' because it is being used by another process. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.IO.IOException: The process cannot access the file 'C:\Users\User1\Documents\Visual Studio 2015\WebSites\MusicStore\App_Code\ConnectionClassGuitarItems.cs' because it is being used by another process. Source Error: Line 184: } Line 185: Line 186: System.IO.File.WriteAllText(@"C:\Users\User1\Documents\Visual Studio 2015\WebSites\MusicStore\App_Code\ConnectionClassGuitarItems.cs", result); Line 187: } Line 188: } Source File: c:\Users\User1\Documents\Visual Studio 2015\WebSites\MusicStore\Pages\OverviewGuitarData.aspx.cs Line: 186 Stack Trace: [IOException: The process cannot access the file ' C:\Users\User1\Documents\Visual Studio 2015\WebSites\MusicStore\App_Code\ConnectionClassGuitarItems.cs' because it is being used by another process.] System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) +216 System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) +1326 System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) +66 System.IO.StreamWriter.CreateFile(String path, Boolean append, Boolean checkHost) +73 System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize, Boolean checkHost) +73 System.IO.File.InternalWriteAllText(String path, String contents, Encoding encoding, Boolean checkHost) +64 System.IO.File.WriteAllText(String path, String contents) +43 Pages_OverviewData.RemoveConnectionClassGuitarItems(String name) in c:\Users\User1\Documents\Visual Studio 2015\WebSites\MusicStore\Pages\OverviewGuitarData.aspx.cs:186 Pages_OverviewData.GuitarBrandsGridViewBtn_Click(Object sender, EventArgs e) in c:\Users\User1\Documents\Visual Studio 2015\WebSites\MusicStore\Pages\OverviewGuitarData.aspx.cs:73 System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9696694 System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +204 System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +12 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +15 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1639 Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.6.1055.0What I have tried:protected void GuitarBrandsGridViewBtn_Click(object sender, EventArgs e) { Button btn = sender as Button; GridViewRow gridrow = btn.NamingContainer as GridViewRow; int id = Convert.ToInt32(GuitarBrandsGridView.DataKeys[gridrow.RowIndex].Value.ToString()); string name = GuitarBrandsGridView.Rows[gridrow.RowIndex].Cells[4].Text; con1.Open(); cmd1.CommandText = "DELETE FROM [guitarBrands] WHERE id=" + id; cmd1.Connection = con1; int a = cmd1.ExecuteNonQuery(); con1.Close(); if (a > 0) { bindgridviewguitarbrands(); } RemoveAddGuitarClass(name); RemoveOverviewGuitarDataCode(name); RemoveConnectionClassGuitarItems(name); RemoveOverviewGuitarDataASPX(name); System.IO.File.Delete(@"C:\Users\User1\Documents\Visual Studio 2015\WebSites\MusicStore\Pages\GuitarItems" + id + ".aspx"); System.IO.File.Delete(@"C:\Users\User1\Documents\Visual Studio 2015\WebSites\MusicStore\Pages\GuitarItems" + id + ".aspx.cs"); }protected void RemoveAddGuitarClass(string name) { int counter = 0; string line; // Read the file and display it line by line. System.IO.StreamReader file1 = new System.IO.StreamReader(@"C:\Users\User1\Documents\Visual Studio 2015\WebSites\MusicStore\App_Code\AddGuitarClass.cs"); while ((line = file1.ReadLine()) != null) { if (line.Contains(" case \"" + name + "\":")) { break; } counter += 1; } file1.Close(); RemoveAddGuitarClassCont(counter); } protected void RemoveAddGuitarClassCont(int counter) { int removeAt = counter;//or any thing you want removeAt -= 1; int linesToRemove = 20; //or any thing you want string s = System.IO.File.ReadAllText(@"C:\Users\User1\Documents\Visual Studio 2015\WebSites\MusicStore\App_Code\AddGuitarClass.cs"); List<string> arr = s.Split("\n".ToCharArray()).ToList(); string result = ""; for (int i = 0; i < linesToRemove; i++) { arr.RemoveAt(removeAt); result = ""; foreach (string str in arr) { result += str + "\n"; } System.IO.File.WriteAllText(@"C:\Users\User1\Documents\Visual Studio 2015\WebSites\MusicStore\App_Code\AddGuitarClass.cs", result); } } protected void RemoveOverviewGuitarDataCode(string name) { int counter = 0; string line; // Read the file and display it line by line. System.IO.StreamReader file2 = new System.IO.StreamReader(@"C:\Users\User1\Documents\Visual Studio 2015\WebSites\MusicStore\Pages\OverviewGuitarData.aspx.cs"); while ((line = file2.ReadLine()) != null) { if (line.Contains(" //Start of Gridview Code for " + name + " Guitars")) { break; } counter += 1; } file2.Close(); RemoveOverviewGuitarDataCodeCont(counter); } protected void RemoveOverviewGuitarDataCodeCont(int counter) { int removeAt = counter;//or any thing you want removeAt -= 1; int linesToRemove = 41; //or any thing you want string s = System.IO.File.ReadAllText(@"C:\Users\User1\Documents\Visual Studio 2015\WebSites\MusicStore\Pages\OverviewGuitarData.aspx.cs"); List<string> arr = s.Split("\n".ToCharArray()).ToList(); string result = ""; for (int i = 0; i < linesToRemove; i++) { arr.RemoveAt(removeAt); result = ""; foreach (string str in arr) { result += str + "\n"; } System.IO.File.WriteAllText(@"C:\Users\User1\Documents\Visual Studio 2015\WebSites\MusicStore\Pages\OverviewGuitarData.aspx.cs", result); } } protected void RemoveConnectionClassGuitarItems(string name) { int counter = 0; string line; // Read the file and display it line by line. System.IO.StreamReader file3 = new System.IO.StreamReader(@"C:\Users\User1\Documents\Visual Studio 2015\WebSites\MusicStore\App_Code\ConnectionClassGuitarItems.cs"); while ((line = file3.ReadLine()) != null) { if (line.Contains(" public static ArrayList GetItems" + name + "(string itemCategory)")) { break; } counter += 1; } file3.Close(); RemoveConnectionClassGuitarItemsCont(counter); } protected void RemoveConnectionClassGuitarItemsCont(int counter) { int removeAt = counter;//or any thing you want removeAt -= 2; int linesToRemove = 44; //or any thing you want string s = System.IO.File.ReadAllText(@"C:\Users\User1\Documents\Visual Studio 2015\WebSites\MusicStore\App_Code\ConnectionClassGuitarItems.cs"); List<string> arr = s.Split("\n".ToCharArray()).ToList(); string result = ""; for (int i = 0; i < linesToRemove; i++) { arr.RemoveAt(removeAt); result = ""; foreach (string str in arr) { result += str + "\n"; } System.IO.File.WriteAllText(@"C:\Users\User1\Documents\Visual Studio 2015\WebSites\MusicStore\App_Code\ConnectionClassGuitarItems.cs", result); } } protected void RemoveOverviewGuitarDataASPX(string name) { int counter = 0; string line; // Read the file and display it line by line. System.IO.StreamReader file4 = new System.IO.StreamReader(@"C:\Users\User1\Documents\Visual Studio 2015\WebSites\MusicStore\Pages\OverviewGuitarData.aspx"); while ((line = file4.ReadLine()) != null) { if (line.Contains(" <h3>" + name + " Guitar Items Data</h3>")) { break; } counter += 1; } file4.Close(); RemoveOverviewGuitarDataASPXCont(counter); } protected void RemoveOverviewGuitarDataASPXCont(int counter) { int removeAt = counter;//or any thing you want removeAt -= 2; int linesToRemove = 44; //or any thing you want string s = System.IO.File.ReadAllText(@"C:\Users\User1\Documents\Visual Studio 2015\WebSites\MusicStore\App_Code\ConnectionClassGuitarItems.cs"); List<string> arr = s.Split("\n".ToCharArray()).ToList(); string result = ""; for (int i = 0; i < linesToRemove; i++) { arr.RemoveAt(removeAt); result = ""; foreach (string str in arr) { result += str + "\n"; } System.IO.File.WriteAllText(@"C:\Users\User1\Documents\Visual Studio 2015\WebSites\MusicStore\App_Code\ConnectionClassGuitarItems.cs", result); } }推荐答案看起来你正在尝试动态更改代码网站,这根本不会工作,是一个非常糟糕的主意。而不是试图解决这个问题,重新设计网站的工作原理,这样您就可以获得所需的动态数据和功能,而无需重新编写类编译类文件。您正在做的事情根本不是网站的工作方式。It looks like you are trying to dynamically change the code of the web site and that's simply not going to work and is a very bad idea. Rather than trying to solve this issue re-architect how the site works such that you get the dynamic data and functionality you need without the need to re-write\compile the class files. What you're doing is simply not how websites are intended to work.如果您正在尝试使用CMS,那么这种代码可能会让您走上正确的道路。所以这里我有一个名为ShowCategory.aspx的页面,它只是在aspx页面中有这个页面If you're trying to do a CMS then this kind of code might get you on the right path. So here I have a page called ShowCategory.aspx that simply has this in the aspx page<asp:Content ID="Content3" ContentPlaceHolderID="MainContent" runat="server"> Category: <%=Request.QueryString["cat"] %></asp:Content> 注意我正在使用默认的WebApp模板,因此它是一个内容页面,其中包含母版页。在global.asax文件中,我这样做Note I'm using the default WebApp template so it's a content page with a Master page. In the global.asax file I do thisvoid Application_BeginRequest(object sender, EventArgs e){ HttpContext context = base.Context; string page = Path.GetFileNameWithoutExtension(context.Request.Url.LocalPath); if (page.Equals("ShowCategory", StringComparison.InvariantCultureIgnoreCase)) { return; } context.RewritePath("~/ShowCategory.aspx?cat=" + page);} 这是非常基本的,它采用网址的页面,它保持网址完整但页面是实际服务的是ShowCategory.aspx,它在查询字符串上传递请求的名称。所以如果我导航到 /test.aspx 我会看到 ShowCategory:test 如果我导航到 /MyFolder/Cakes.aspx 我会看到 ShowCategory:Cakes 我只显示传递的参数,实际上你的代码会从数据库中获取蛋糕并显示它们,或者页面必须做的任何事情。 所以你需要做的是通过更复杂的网址分析来扩展这个概念,以找出你真正想要展示的内容,或者你是否想要正常处理页面。例如,你可以决定这样的事情This is very basic, it take the page of the url and it keeps the url intact but the page is actually serves is ShowCategory.aspx and it passes the requested name on the querystring. So if I navigate to/test.aspxI'll seeShowCategory: testif I navigate to/MyFolder/Cakes.aspxI'll seeShowCategory: CakesI'm only showing the parameter passed, in reality your code would then get a cakes from the database and show them, or whatever it is the page has to do.So what you need to do is expand that concept with more sophisticated url analysis to work out what you actually want to show, or if you want to have the page handled as normal. eg you could decide on something like thisvoid Application_BeginRequest(object sender, EventArgs e){ HttpContext context = base.Context; string[] seg = context.Request.Url.LocalPath.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries); if (seg.Length < 2) { return; } if (seg[0].Equals("Category", StringComparison.InvariantCultureIgnoreCase)) { context.RewritePath("~/ShowCategory.aspx?cat=" + seg.Last()); }} 现在所有页面请求都正常处理,除非它是/ category / 在那个例子中我们展示了ShowCategory页面。显然你可以进一步扩展,所以如果它是/ search / something你可以显示一个Search.aspx页面,或者它是/ category / ProductName你应该显示一个Product.aspx页面,它将显示该详细信息产品。 无论如何,这是如何开始构建CMS系统的基础知识。so now all page requests are handled as normal, unless it is "/category/something" and in that instance we show the ShowCategory page. Obviously you can extend that further so if it was "/search/something" you could show a Search.aspx page, or if it was "/category/ProductName" you should show a Product.aspx page that will show the details of that product.Anyway, that's the basics of how you can start to build a CMS system.查看错误消息:Look at the error message:The process cannot access the file 'C:\Users\User1\Documents\Visual Studio 2015\WebSites\MusicStore\App_Code\ConnectionClassGuitarItems.cs' because it is being used by another process. 该文件正在使用中。您的代码中的其他位置可能会在文件上打开一个流,但无法关闭它。因此,文件被锁定,您无法写入并获得异常。 检查其余代码,而不是手动关闭流,使用使用每次阻止以确保它自动为您关闭和处置:The file is in use. The chances are that somewhere else in your code is opening a stream on the file, but is failing to close it. As a result the file is locked and you cannot write to it and get the exception.Check the rest of your code, and instead of manually closing the stream, use a using block each time to ensure that it is Closed and Disposed automatically for you:using (System.IO.StreamReader file = new System.IO.StreamReader(@"C:\Users\User1\Documents\Visual Studio 2015\WebSites\MusicStore\App_Code\ConnectionClassGuitarItems.cs")){ while ((line = file.ReadLine()) != null) { if (line.Contains(" public static ArrayList GetItems"+name+"(string itemCategory)")) { break; } counter += 1; }}在创建流的任何地方重复此操作,您的问题可能会消失。Repeat that everywhere you create a stream, and your problem will likely go away. 这篇关于ASP.NET中“/”应用程序中的服务器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-22 08:55